Skip to content Skip to sidebar Skip to footer

Python Subprocess.popen Blocks With Shell And Pipe

Running the following command: yes | head -1, from the shell, outputs y. Using python's subprocess module to call it with a shell hangs indefinitely: import subprocess arg = 'yes

Solution 1:

So it turns out that this is caused due to a known issue with python not resetting signal handlers before exec in the subprocess module.

This is the same problem that causes 'yes' reporting error with subprocess communicate(), except with GNU yes, the EPIPE returned by a write causes the program to abort, whereas with BSD yes (which is used on OS X as far as I can tell), the return code of the write is not checked, so without a SIGPIPE, yes will not terminate.

It can be fixed by backporting the reset_signals code, as in the above linked question:

def restore_signals():
    signals = ('SIGPIPE', 'SIGXFZ', 'SIGXFSZ')
    for sig in signals:
        if hasattr(signal, sig):
            signal.signal(getattr(signal, sig), signal.SIG_DFL)

then setting preexec_fn=restore_signals in the Popen call.

Post a Comment for "Python Subprocess.popen Blocks With Shell And Pipe"