CPU usage of Python interpreter doing empty while loop under XP

Peter Hansen peter at engcorp.com
Tue Apr 27 14:24:03 EDT 2004


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



More information about the Python-list mailing list