subprocesses and deadlocks

Simon Forman rogue_pedro at yahoo.com
Sun Aug 6 12:19:36 EDT 2006


betatim at gmail.com wrote:
> 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

When GvR wrote that around a decade ago, tempfile.mktemp() had not yet
been deprecated.  It returns "an absolute pathname of a file that did
not exist at the time the call is made".  It does not create a file,
you have to do that yourself.

You're quoting the docs for tempfile.TemporaryFile(). It returns a
"file (or file-like) object", and I'd assume that you would have to
pass this object around without closing it in order to use it in the
manner described in GvR's post.


http://docs.python.org/lib/module-tempfile.html




More information about the Python-list mailing list