On some machine, for the following piece of code
p = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr = subprocess.STDOUT);
out, err = p.communicate()
This script would just hang at p.communicate()
and does not return.
After I manually run the command, I finally see the error messages.
Why is this, and how should I solve it?
Thanks.
尝试stderr = subprocess.PIPE
不是stderr = subprocess.STDOUT
I guess your program never ends?
when you call communicate() it does different things under different os. But it always waits for the launched process to exit on its own eg. calls p.wait(). p.wait() terminates only if the rocess terminates.
Solutions:
way
def main():
## here is you program code
if __name__ == '__main__':
import thread, sys, time
_id = thread.start_new(main, ())
time.sleep(1)
t = time.time() + 100 # execute maximum 100 + 1 seconds
while t > time.time() and _id in sys._current_frames():
time.sleep(0.001)
If you want to look at the source of subprocess you can find it at subprocess.__file__
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.