Trapping the segfault of a subprocess.Popen

Pierre GM pierregmcode at gmail.com
Wed Apr 6 11:20:22 CEST 2011


All,

I need to run a third-party binary from a python script and retrieve
its output (and its error messages). I use something like
>>> process = subprocess.Popen(options, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>>> (info_out, info_err) = process.communicate()
That works fine, except that the third-party binary in question
doesn't behave very nicely and tend to segfaults without returning any
error. In that case, `process.communicate` hangs for ever.

I thought about calling a `threading.Timer` that would call
`process.terminate` if `process.wait` doesn't return after a given
time... But it's not really a solution: the process in question can
sometimes take a long time to run, and I wouldn't want to kill a
process still running.
I also thought about polling every x s and stopping when the result of
a subprocess.Popen(["ps","-p",str(initialprocess.pid)],
stdout=subprocess.PIPE) becomes only the header line, but my script
needs to run on Windows as well (and no ps over there)...

Any suggestion welcome,
Thx in advance
P.



More information about the Python-list mailing list