subprocesses and deadlocks

betatim at gmail.com betatim at gmail.com
Sun Aug 6 10:39:21 EDT 2006


Hi,

there are many ways of solving the problem of finite buffer sizes when
talking to a subprocess. I'd usually suggest using select() but today I
was looking for a more readable/understandable way of doing this. Back
in 1997 Guido himself posted a very nice solution, write your input to
a temporary file and then read that from your new process. His posting
can be found here:
http://groups.google.com/group/comp.lang.python/tree/browse_frm/thread/2b31d990a8613d93/17d3dea9089aad00?rnum=1&q=subprocess+deadlock&_done=%2Fgroup%2Fcomp.lang.python%2Fbrowse_frm%2Fthread%2F2b31d990a8613d93%2F63b0a786d87ba23b%3Flnk%3Dgst%26q%3Dsubprocess+deadlock%26rnum%3D6%26#doc_63b0a786d87ba23b

Being a bit puzzled over this usage of tempfile I read its
documentation and as expected it says:

[...] The file is created using mkstemp. It will be destroyed as soon
as it is closed (including an implicit close when the object is garbage
collected). [...] your code should not rely on a temporary file created
using this function having or not having a visible name in the file
system.

so how was Guido planning to get the contents of the file after closing
it? Should we do a tf.flush() instead of the close to ensure everything
is written, then read from it, using subprocess.Popen(....,stdin=tf,..)
and only close it afterwards?

Is it correct to assume that a named temporary file will be (sometimes)
accesible while it has not been closed yet?

cheers,
tim




More information about the Python-list mailing list