Dead end when dealing with multiple children
This is a continuation of the previous question:
I have parent.py
:
from sys import argv
from random import randrange
from subprocess import Popen, PIPE
lines = int(argv[1])
procs = int(argv[2])
cmd = ["python", "child.py"]
children = list()
for p in range(procs):
children.append([0,Popen(cmd, stdin=PIPE, stdout=PIPE)])
for i in range(lines):
child = children[randrange(procs)]
child[0] += 1
child[1].stdin.write("hello\n")
for n,p in children:
p.stdin.close()
out = p.stdout.read()
p.stdout.close()
exitcode = p.wait()
print n,out,exitcode
assert n == int(out)
assert lines == sum(n for n,_ in children)
and a child.py
:
import sys
l = list()
for line in sys.stdin:
l.append(line)
sys.stdout.write(str(len(l)))
When I create one child, it works great:
$ python parent.py 100 1
100 100 0
However, a multiplayer dead end:
$ python parent.py 100 2 &
[1] 59492
$ strace -p 59492
Process 59492 attached - interrupt to quit
read(5, ^C <unfinished ...>
Process 59492 detached
$ pstree -p 59492
python(59492)-+-python(59494)
`-python(59495)
$ strace -p 59494
Process 59494 attached - interrupt to quit
read(0, ^C <unfinished ...>
Process 59494 detached
$ strace -p 59495
Process 59495 attached - interrupt to quit
read(0, ^C <unfinished ...>
Process 59495 detached
Why do the kids keep going read()
even after I close them stdin
?
PS. Simply changing the closing of all children of stdins before reading from any of them solves the problem :
$ python parent.py 10000 4
2486 2486 0
2493 2493 0
2531 2531 0
2490 2490 0
WHY?!
+3
source to share
2 answers