Multi-threaded printing and IOErrors in Windows

David Bolen db3l at fitlinxx.com
Fri Apr 23 15:13:11 EDT 2004


"Roger Binns" <rogerb at rogerbinns.com> writes:

> When using py2exe, the stdout doesn't go anywhere anyway
> (it is gui app not a console app).
> 
> The bizarre thing is that the IOError is very infrequent,
> but very annoying when it happens.

Have you tried replacing sys.stdout with your own object that just
throws away anything it is given?  If you're not getting the output
anywhere anyway, it would avoid any attempt to actually use the
underlying I/O facilities.  You could conditionalize this code based
on whether or not you were running under py2exe.

I know there can be an issue under Windows under some startup
scenarios where you don't actually have opened O/S filesystem handles
for the handles underlying stdin/stdout (which can cause this sort of
problem), such as running as a service, but haven't normally had this
problem with GUI applications.  Although it may matter on how you are
starting the application (from a command line, the start menu, as a
service, etc...), since it will inherit its parent environment.

If that's the problem, I'd expect it to happen any time actual I/O was
attempted.  However, since there is probably some buffering going on
beneath the level of print within the underlying C library, maybe the
randomness of the error you are seeing is due to how long it takes to
print enough data to cause the C library to attempt to flush the data
to the underlying O/S filesystem handle.  If that were the case, doing
some test prints in your code of very long strings (the C buffer is
probably anywhere from 2-8K) should be able to reproduce the problem
more reliably.

-- David



More information about the Python-list mailing list