Doubts related to subprocess.Popen()

Mark Wooding mdw at distorted.org.uk
Tue Jan 20 12:29:48 EST 2009


"Diez B. Roggisch" <deets at nospam.web.de> writes:

> Usually, each new process has three file-descriptors associated with
> it - stdin,stdout and stderr.
>
> So when you span a process, the overall count of FDs should increase
> by three.

Yes, but that's irrelevant.  There are two file limits which are
relevant:

  * the per-process limit on file descriptors -- basically, the largest
    number which can be a file descriptor -- and

  * the overall number of files which can be open at a time.

Note that, in Unix, the same file can be referred to by many different
descriptors in many different processes, and fork(2), exec(2) and dup(2)
don't change the number of files open.  However, dup(2) allocates a new
descriptor in the calling process, so it may hit the per-process limit.

> Additionally, there are more FDs created if you chose to pipe
> communication between the child-process and the parent.

And these are the ones I mentioned.

OK, in more detail: each pipe(2) call allocates two files (one for each
end) and two file descriptors (one for each file).  If you call Popen
with PIPE specified for each of stdin, stdout and stderr, then that's a
total of six descriptors and six files.  But the parent will close half
of them after calling fork(2) (freeing three descriptors), and the child
will close all six after dup2(2)-ing them over the descriptors 0, 1, and
2.  The net result is:

  * six new files in the global file table, and
  * three new descriptors in the parent.

(The child ends up with no new descriptors at the end of all this.)

-- [mdw]



More information about the Python-list mailing list