给出以下代码:
try:
subprocess.Popen(ExternalProcess, stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True).communicate()
except KeyboardInterrupt:
exit(0)
If during the execution of ExternalProcess
(which is not a python script) one presses the Ctrl+C command, what exactly is going on?
Can I be sure for a 100% that in this scope, if I press the Ctrl+C, it will always get into the 'except' even if it happens during the execution of ExternalProcess
?
还是取决于外部流程如何处理?
As far as I understand, once you fork/exec a process, it inherits the parent's process group http://en.wikipedia.org/wiki/Process_group. That means, that SIGINT
(which is the result of Ctrl+C and the cause of KeyboardInterrupt
) will be sent to both the child and the parent.
这是一个例子:
File a.py
:
import subprocess
try:
subprocess.Popen("python b.py".split()).communicate()
except KeyboardInterrupt:
print "a.py got ctrl-c"
File b.py
try:
while True: pass
except KeyboardInterrupt:
print "b.py got ctrl-c"
现在运行它并停止:
> python a.py
^Cb.py got ctrl-c
a.py got ctrl-c
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)