I've created on my desktop a file flooder.py containing just the following: ################## import sys, os progress=open(r"C:\Users\v-pascha\Desktop\STDERR.txt","w") for i in range(101): print str(i)*20 progress.write( str(i)+"\n" ) progress.close() ################## and a file receiver.py containing just : ################ import sys, os, subprocess #os.system("pause") subprocess.Popen(r"python C:\Users\v-pascha\Desktop\flooder.py",stdin=subprocess.PIPE,stdout=subprocess.PIPE) #,stdin=subprocess.PIPE,stdout=subprocess.PIPE #os.system("pause") ##################### And when I launch receiver.py, I get a crash with that (not explicit) message : "close failed in file object destructor: Error in sys.excepthook: Original exception was:" The crash doesn't happen if I don't redirect the stdout and stdin of the child process. So it seems something weird happens when subprocess tries to redirect the child's I/O to the PIPEs...
Probably this happens because receiever.py doesn't wait for flooder.py termination, and pipe end is closed when recieiver.py terminates. Does this code work for you? p = subprocess.Popen("python flooder.py",stdin=subprocess.PIPE,stdout=subprocess.PIPE) p.wait()
This happens because when flooder.py terminates, its stdout will be closed, but another pipe end in receirver.py process is already closed, so Python\sysmodule.c(1098): _check_and_flush (FILE *stream) In this function, fflush() fails. The reason why error message is not helpful is probably this close function is called in interpreter termination process, other modules/objects to do so are already destroyed. Maybe this error message can be improved in some way, but I'm not sure.
Thansk a lot for reviewing the problem Indeed, "p.wait()" seems to do the trick in this case. Is there any global way to avoid such pipe problems ? I mean, in any case, one end of each pipe has to be closed before the other end, so such errors might occur with the child process' stdin (in case the child dies first and the parent flushes then its pipe toward the child's stdin) or with its stdout (if the parent dies first and the child flushes then its stdout). Or are we sure that there won't be errors as long as children die before the parent process ?