473,320 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

CPU usage of Python interpreter doing empty while loop under XP

When you run an empty while loop under Python [with the
interpreter at its default Normal Priority], it slows
down the rest of the system. Is this normal? And should
it be something that needs to be corrected?
[Note: I'm using ActiveState Python 2.3.2 Build 232]
Jul 18 '05 #1
7 7020
On 27 Apr 2004 04:45:21 GMT, "Jon Perez" <jb********@wahoo.com>
declaimed the following in comp.lang.python:
When you run an empty while loop under Python [with the
interpreter at its default Normal Priority], it slows
down the rest of the system. Is this normal? And should
it be something that needs to be corrected?
Normal compared to what? The same loop in C? Assembly?

An empty loop would, to my mind, indicate that there are no OS
calls being made which could allow for a task switch. This would mean
that the entire time quantum will be used for that process, before a
forced OS preemption, followed by whatever other process you are
running. If that other process performs some sort of I/O (or other
system call) the odds are your empty loop will immediately regain
control.

And since a windowing system does LOTS of I/O, there is lots of
opportunity for the empty loop to suck down CPU cycles. Stick a
sleep(0) inside it, and no doubt you will see performance return to
"normal". Even though the 0 means no actual delay is wanted, the task
swap will occur, giving the OS a chance to respond to events.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #2
Dennis Lee Bieber wrote:
Stick a sleep(0) inside it, and no doubt you will see performance
return to "normal". Even though the 0 means no actual delay is wanted,
the task swap will occur, giving the OS a chance to respond to events.


Great, it worked.

So

while 1: sleep(0)

is a better alternative to

while 1: pass
(I'm using this for debugging purposes...)
Jul 18 '05 #3
Jon Perez wrote:
Dennis Lee Bieber wrote:
Stick a sleep(0) inside it, and no doubt you will see performance
return to "normal". Even though the 0 means no actual delay is wanted,
the task swap will occur, giving the OS a chance to respond to events.

Great, it worked.

So

while 1: sleep(0)

is a better alternative to

while 1: pass

(I'm using this for debugging purposes...)


How does that help your debugging? That will tell us whether
sleep(0) is a better alternative, or whether there is really
something else you should be doing instead.

(I have never had occasion to use "while 1: pass" in Python, ever.)

-Peter
Jul 18 '05 #4
Peter Hansen wrote:
How does that help your debugging? That will tell us whether
sleep(0) is a better alternative, or whether there is really
something else you should be doing instead.

(I have never had occasion to use "while 1: pass" in Python, ever.)

-Peter


I'm trying to debug an event-driven framework I wrote myself and
there are some iffy situations where a quick and dirty way to halt
execution and view the state of some variables with print is to
insert a while 1: pass (or sleep(0)).

The only reason sleep(0) is better than pass in my situation
is that when I try to do other things under the OS before I've
had a chance to break out of the program, I don't get a slowdown.
Jul 18 '05 #5
Jon Perez wrote:
Peter Hansen wrote:
How does that help your debugging? That will tell us whether
sleep(0) is a better alternative, or whether there is really
something else you should be doing instead.

(I have never had occasion to use "while 1: pass" in Python, ever.)


I'm trying to debug an event-driven framework I wrote myself and
there are some iffy situations where a quick and dirty way to halt
execution and view the state of some variables with print is to
insert a while 1: pass (or sleep(0)).


Good, then what you are really looking for is to get away from the
print statement and learn to use "import pdb; pdb.set_trace()".

This will drop you into a convenient interpreter prompt right in
the frame where it executes (well, one level down, so just type "r"
to return from the set_trace() call to the calling level). From
this prompt you can inspect any variable in your program, execute
code by single-stepping, and so forth.

Very handy, and it completely obsoletes the "while 1: pass" idea. :-)

-Peter
Jul 18 '05 #6
Peter Hansen wrote:
Good, then what you are really looking for is to get away from the
print statement and learn to use "import pdb; pdb.set_trace()".

This will drop you into a convenient interpreter prompt right in
the frame where it executes (well, one level down, so just type "r"
to return from the set_trace() call to the calling level). From
this prompt you can inspect any variable in your program, execute
code by single-stepping, and so forth.

Very handy, and it completely obsoletes the "while 1: pass" idea. :-)

-Peter


Thanks for the tip, I'll keep it in mind for next time, although
for this thing I'm doing, I might not be able to use pdb because
the event driven framework I'm doing (built on top of Fredrik
Lundh's console) has to do with a text user interface and I can't
have things messing up the screen which is what would happen if I
used the pdb technique.

To illustrate how careful I have to be about preserving what's on
the screen, I have a debugmsg() function which saves the current
cursor position, prints out variable values (or whatever I like)
to a small unused corner of the screen, then restores the cursor
position. Because it's an event driven framework, program flow
is hard to predict, so there are situations where I need the
program to effectively 'hang' at certain portions (a kind of breakpoint
if you will) so I can see the debug message as well as the current
state of the screen without it being overwritten immediately by
either pdb output or succeeding code that does a screen clear or
refresh.

I would be able to use pdb if Console allowed you to separate
the screen buffer for the program output with that of the command
line from which you invoked said program (where the error messages
and pdb output should go to), but there doesn't seem to be a way
to do that.
Jul 18 '05 #7
Jon Perez wrote:
I would be able to use pdb if Console allowed you to separate
the screen buffer for the program output with that of the command
line from which you invoked said program (where the error messages
and pdb output should go to), but there doesn't seem to be a way
to do that.


There are a variety of ways to get remote debugging and
remote interpreter prompts, some supported by the standard
library modules (such as the cmd module). I also note
in the pdb module documentation that the Pdb class itself
is explicitly extensible, suggesting you could rather
easily extend it to support external interfaces other than
the local console. And a quick Google search suggests that
the functionality you needed was added (probably using the
time machine again) around last August:

http://mail.python.org/pipermail/pat...st/013227.html

(Probably your current approach will suffice for now, but
consider this for the next time, as it will give you
significantly enhanced debugging abilities, including the
possibly useful ability to launch a debugger prompt
as soon as an exception is raised, right where the exception
is caught.)

-Peter
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Anon | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all I am a beginner teaching myself python, and I am enjoying it immensely :) As a language it is great, I real treat to study, I actually...
13
by: Rolf Magnus | last post by:
Hi, I would like to embed a python interpreter within a program, but since that program would be able to automatically download scripts from the internet, I'd like to run those in a restricted...
6
by: Engineer | last post by:
I'm looking for a Python interpreter written in BASIC, preferably Visual Basic, and one written in VBA would be best of all. An alternative would be a Python-2-Basic compiler. Unfortunately I...
12
by: Rex Eastbourne | last post by:
Hi, I'm interested in running a Python interpreter in Emacs. I have Python extensions for Emacs, and my python menu lists "C-c !" as the command to run the interpreter. Yet when I run it I get...
6
by: neokosmos | last post by:
I was wondering what the approximate amount of memory needed to load a Python interpreter (only, no objects, no scripts, no nothing else) in a Linux 2.6 environment. According to ps, it appears to...
7
by: sam | last post by:
dear all, having spent the last couple of weeks getting to grips with python on windows, i am in the position of trying to make the transition to my newly arrived ultra 20. however, although...
3
by: morris.slutsky | last post by:
So every now and then I like to mess around with hobby projects - I often end up trying to write an OpenGL video game. My last attempt aborted due to the difficulty of automating game elements and...
0
by: has | last post by:
Hi all, need a little bit of advice on dynamically binding an embedded Python interpreter. First, the code for anyone that wants a look: ...
6
by: ssecorp | last post by:
def str_sort(string): s = "" for a in sorted(string): s+=a return s if i instead do: def str_sort(string):
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.