Python wait () subprocess behaves differently on mavericks and Yosemite
I recently upgraded to Yosemite. And some Python scripts hang, which was used to work with Mavericks. My version is 2.7.8. I created a test case:
import subprocess
cat = subprocess.Popen(['top', '-l', '1'],
stdout=subprocess.PIPE,
)
cat.wait()
Powered by Maverics, but hangs on Yosemite. When I interrupt Yosemite, I see the following traceback.
Traceback (most recent call last):
File "test.py", line 5, in <module>
cat.wait()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1376, in wait
pid, sts = _eintr_retry_call(os.waitpid, self.pid, 0)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 476, in _eintr_retry_call
return func(*args)
KeyboardInterrupt
Any hint as to what I am doing wrong?
source to share
It looks like you need to call communicate()
. From Python documentation :
Popen.wait()
Wait for the child process to complete. Set and return the attribute
returncode
.Warning: This will stall when used
stdout=PIPE
and / orstderr=PIPE
, and the child process generates enough output to the pipe so that it blocks waiting for the OS buffer to receive more data. usecommunicate()
to avoid it.
source to share