Debugging on windows via print statements -- reliable?

Peter Hansen peter at engcorp.com
Sun Mar 24 23:20:42 EST 2002


Nick Arnett wrote:
> 
> As I try to track down why my code is getting stuck (in a situation where it
> could take ages to step through in the debugger), I'm using print statements
> so that I can see where it's just been before it freezes.  Trouble is, I'm
> not seeing much consistency in when (how many iterations) or where (in the
> code) it stops responding.
> 
> Now I'm wondering if way of creating signposts is reliable.  Is it possible
> that it's actually hanging up somewhere without actually outputting some of
> my print statements, even though they've been processed?  Sometimes, I'm
> only getting part of the output of a print statement, which seems truly
> weird.  If I had multiple threads, perhaps I could be clobbering the thread
> that's printing my signposts, but I'm not...
> 
> Is there something on Windows (Win2K, to be exact) that I need to do to make
> sure that print statements are actually output as soon as the statement is
> executed.
> 
> Thanks in advance...

I don't recall if print flushes its output after each call, but if you
want to be certain it does, try calling sys.stdout.flush() after your
print calls.  This is a console app, right?, and you aren't redirecting
the output to a file?

By the way, how can Python code get "stuck"?  Is it a logic flaw,
or are you talking about the kind of "stuck" that used to happen,
in the "old days", when we were always writing code in languages
which would actually crash the computer?

-Peter



More information about the Python-list mailing list