Jon Brandvein <jon.brandv...@gmail.com> added the comment:

It turns out the file output was flushing due to garbage collection. When I 
created and held a global reference to it, it ceased to flush. Clearly, 
reassigning sys.stdout also held a reference to it. So it wasn't any kind of 
special sys.stdout-specific logic.

I tried using os.fsync to determine whether data was being saved in an OS-level 
buffer. But since the OS may be free to ignore fsync, it's kind of worthless 
for this purpose.

I also found that under Python 2.x, even a low-level exit like os._exit or 
multiprocessing.win32.ExitProcess, called from within a user-level function in 
the child, caused flushing.

My best guess is that
  1. Python 2.x is not buffering data at the Python level. I can't see how it 
could be and still flush it out when calling _exit().
  2. Python 3.x is buffering at the Python level, and the Python File object 
needs to be destroyed or explicitly flushed before the hard low-level exit() in 
forking.py.

The solutions I can think of for Python 3.x are:
  1. Replace "exit = win32.ExitProcess" with "exit = sys.exit". All outstanding 
file objects will be destroyed and flushed naturally as the interpreter is torn 
down.
  2. Add an explicit stdout/stderr flush where appropriate in forking.py and 
process.py, to ensure tracebacks get written and to match the unix behavior. 
Leave it to the user to worry about flushing their own streams.
  3. Continue to use win32.ExitProcess, but add some kind of mechanism for 
walking through all existing Python File objects and flushing/destroying them. 
This was a fleeting thought; it really amounts to reimplementing the behavior 
of destructing the Python interpreter.

I'd really like to hear if there are good reasons for why (1) isn't how it's 
done currently. I'd also like to hear an explanation of Python 2.x's buffering.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue13812>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to