Why python subprocess.Popen need close_fds parameter?

Question:

Creating a subprocess seems to need to close some fd from caller, e.g., if caller opened fd 0,1,2(stdin,out,err) and fd=3(file named “a.txt”), and subprocess.Popen sets “close_fd=True”, like

p=subprocess.Popen(cmd,shell=True,close_fds=True,stdout=None...

Does it mean:

(1) fd 0-3 are closed in subprocess?

(2) if fd 0-3 are closed, how could subprocess print lines to screen, or communicate its input/output with caller by :

p.communicate()

Just a bit confused. Explanations?

Asked By: Hind Forsum

||

Answers:

Only 3 is closed. From the Popen docs:

If close_fds is true, all file descriptors except 0, 1 and 2 will be
closed before the child process is executed. (POSIX only).

Answered By: Harvey