Multi-threaded printing and IOErrors in Windows
David Bolen
db3l at fitlinxx.com
Fri Apr 23 15:16:50 EDT 2004
"Roger Binns" <rogerb at rogerbinns.com> writes:
> David Fraser wrote:
> > sys.stdout is not threadsafe - try wrapping each call with a mutext and
> > see if that fixes it...
>
> Having to wrap hundreds of random print statements just using for
> debugging seems really silly. Note that I don't care if the
> output gets garbled. (It doesn't on Mac or Linux ever).
To follow up on this point to my previous reply - I've never found
sys.stdout not to be threadsafe, at least in terms of how I mean
"threadsafe" - that is generating exceptions or crashes.
If instead, threadsafe is meant to imply atomic I/O (guaranteed output
of entire I/O as a unit) which is a broader definition, then yes, it
is definitely true that multiple threads all using sys.stdout may have
their output interleaved, particularly when you are using the 'print'
statement, since a single print statement generates multiple
individual writes to sys.stdout, generally for each argument, as well
as the trailing newline.
-- David
More information about the Python-list
mailing list