Eryk Sun added the comment:

Due to a race condition, the Popen call in the second process is inadvertently 
inheriting the handle for the write end of the pipe that's created for the 
first process. Thus stdout.readline() in the first thread doesn't see EOF until 
that handle is closed. 

For the 2nd process, since you don't need to inherit standard handles, you can 
pass close_fds=True. In general if you do need to inherit standard handles in 
processes that are created concurrently, you can synchronize on a lock to 
ensure Popen properly closes inheritable handles. For example:

    import threading
    import subprocess

    class Popen(subprocess.Popen):
        _execute_lock = threading.Lock()
        def __init__(self, *args, **kwds):
            with self._execute_lock:
                super(Popen, self).__init__(*args, **kwds)

In Python 3 this should be addressed by implementing the suggestion in issue 

nosy: +eryksun

Python tracker <>
Python-bugs-list mailing list

Reply via email to