Re: CPU usage of Python interpreter doing empty while loop under XP

From: Peter Hansen (peter_at_engcorp.com)
Date: 04/27/04


Date: Tue, 27 Apr 2004 14:24:03 -0400

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



Relevant Pages

  • Re: Importing functions that require parameters
    ... As a python module is executed when ... To prohibit execution ... Now the print statement will be executed if you invoke your script from ...
    (comp.lang.python)
  • Re: After RAISERROR executes next line.
    ... If I understand you correctly you want to raise an exception so that the ... print statement is NOT executed?. ... > I am trying to raise an error in MS SQL Server 2000 using the lines ... > But, on execution, it shows the error message and goes to the next line. ...
    (microsoft.public.sqlserver.server)
  • forcing standard output to overwrite same line
    ... I have a lengthy program that has an outer loop, ... print statement that tells me what percent complete that loop is. ... instead just keep overwriting "x". ... output at the end of execution. ...
    (comp.lang.fortran)

Loading