Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why python subprocess.Popen need close_fds parameter?

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?

like image 994
Hind Forsum Avatar asked Oct 23 '25 14:10

Hind Forsum


1 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).

like image 62
Harvey Avatar answered Oct 26 '25 05:10

Harvey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!