如何停止在 Windows 和 Linux 上执行子进程
How to stop a subprocess from execution on Windows and Linux
如何停止来自 运行 的子进程?
我试穿了 windows:
__process.kill()
__process.terminate()
我也试过了:
os.kill(self.__process.pid, signal.SIGTERM)
最后我让它在 while 循环中停止等待子进程中的一个词,我使用的是:
self.__process.communicate(input='quit')
但我不想等待输入。
谢谢。
使用进程组,以便能够向组中的所有进程发送信号。为此,您应该将会话 ID 附加到 spawned/child 进程的父进程。所以现在,当一个信号被发送到进程时,它被传输到这个组的所有子进程。
代码如下:
import os
import signal
import subprocess
# The os.setsid() is passed in the argument preexec_fn so
# it's run after the fork() and before exec() to run the shell.
pro = subprocess.Popen(cmd, stdout=subprocess.PIPE,
shell=True, preexec_fn=os.setsid)
os.killpg(os.getpgid(pro.pid), signal.SIGTERM) # Send the signal to all the process groups
如何停止来自 运行 的子进程?
我试穿了 windows:
__process.kill()
__process.terminate()
我也试过了:
os.kill(self.__process.pid, signal.SIGTERM)
最后我让它在 while 循环中停止等待子进程中的一个词,我使用的是:
self.__process.communicate(input='quit')
但我不想等待输入。 谢谢。
使用进程组,以便能够向组中的所有进程发送信号。为此,您应该将会话 ID 附加到 spawned/child 进程的父进程。所以现在,当一个信号被发送到进程时,它被传输到这个组的所有子进程。
代码如下:
import os
import signal
import subprocess
# The os.setsid() is passed in the argument preexec_fn so
# it's run after the fork() and before exec() to run the shell.
pro = subprocess.Popen(cmd, stdout=subprocess.PIPE,
shell=True, preexec_fn=os.setsid)
os.killpg(os.getpgid(pro.pid), signal.SIGTERM) # Send the signal to all the process groups