I have a GUI python program and a simple error logging system via
import sys
sys.stderr = open("err.log", "w")
and it works mostly fine. The one problem I have is that whenever I encounter a run-time error using Windows 7 and Python 2.7.3, the file err.log gets written only after I close the program. Reading related issues I gather I’d need to perform flush() and os.fsync() to sys.stderr after the error, but I don’t know how to do that easily.
One way would perhaps be to perform flush/fsync after every possible point in the program where a run-time error is possible, but this is obviously not a good solution. The main reason for using this error logging is debugging, so by definition I can’t really know beforehand where I’d need to flush other than at all the possible places where a run-time error is possible. Since there are a lot of such places I’d prefer not to have to try/catch every one of them.
I’m using PyGTK, so there is no main loop visible to me in which I could do the flush/fsync.
Is there any way to tell Python to always perform flush and fsync after an error, or does anyone have other ideas how to solve this (minor) problem?
I agree with syhpoon: using
loggingfor logging. But if you really need your arrangement, look at the thirdbufferingargument toopento disable it:I tried this and it works. In a Python session:
And in another term window:
Then I re-ran the experiment with buffering disabled:
And:
So use
logging, but if not, see if just disabling buffering will give you what you need.