pwtools.common.permit_sigpipe

pwtools.common.permit_sigpipe()[source]

Helper for subprocess.Popen(). Handle SIGPIPE. To be used as preexec_fn.

Notes

Things like:

>>> cmd = r"grep pattern /path/to/very_big_file | head -n1"
>>> pp = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
...                       stderr=subprocess.PIPE)
>>> out,err = pp.communicate()

sometimes end with a broken pipe error: “grep: writing output: Broken pipe”. They run fine at the bash prompt, while failing with Popen. The reason is that they actually “kind of” fail in the shell too, namely, SIGPIPE [1,2]. This can be seen by runing the call in strace “$ strace grep …”. Popen chokes on that. The solution is to ignore SIGPIPE.

References