I noticed that in Python3 subprocesses the default for Popen parameter close_fds
was changed from False
to True
and I wondered what was the reason and whether it is a good practice to almost always set close_fds
to True
(as I'm still using Python 2.7).
I have found a link showing an issue with close_fds=False
.
https://bugs.python.org/issue7213
Unfortunately, it isn't clear to me why it so happens.
import subprocess as sub
p1 = sub.Popen(['cat'], stdin=sub.PIPE, stdout=sub.PIPE, close_fds=False)
p2 = sub.Popen(['grep', 'a'], stdin=p1.stdout, stdout=sub.PIPE, close_fds=False)
p1.stdin.write("aaaaaaaaaaaaaaaa\n")
p1.stdin.close()
p2.stdout.read() # Hangs on Python 2
The program hangs on Python2, does not on Python3 and does not hang at all if close_fds
is set to True
. So I wonder... What was the actual issue there?
EDIT: It hangs on my Python 2.6 and stopped hanging on 2.7