简体   繁体   English

如何停止从终端启动许多子进程的python脚本

[英]How to stop a python script launching many subprocesses from terminal

I have a python script which launches a sequence of subprocesses in a loop: 我有一个python脚本,它在循环中启动一系列子过程:

for c in cmds:
  subprocess.call(c, shell=True)

When the script is running in terminal, I try to stop it with Ctrl + C or Ctrl + D , however, it will continue to launch next subprocess. 当脚本在终端中运行时,我尝试使用Ctrl + CCtrl + D停止它,但是它将继续启动下一个子进程。 How can I terminate the whole python script at once? 如何立即终止整个python脚本?

What likely happens is that ctrl-C is intercepted by you sub-process (running on the same terminal) and never gets to your Python script. 可能发生的情况是ctrl-C被您的子进程(在同一终端上运行)拦截,并且从不进入Python脚本。 What you can do is to check the return value of subprocess.call to see whether the child process was killed by a signal and decide whether you want to stop submitting new processes. 您可以做的是检查subprocess.call的返回值,以查看子进程是否被信号杀死,并决定是否要停止提交新进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM