我有一个使用 subprocess.Popen
启动子进程的 python 脚本。然后子进程启动一个外部命令(在我的例子中,它播放 mp3)。 python脚本需要能够中断子进程,所以我使用了描述的方法here它为子进程提供了自己的 session ID。不幸的是,当我现在关闭 python 脚本时,子进程将继续运行。
如何确保子进程从脚本启动,但在 python 脚本停止时给定不同的 session ID 仍然关闭?
最佳答案
Is there any way to kill a Thread in Python?
并确保将其用作线程
import threading
from subprocess import call
def thread_second():
call(["python", "secondscript.py"])
processThread = threading.Thread(target=thread_second) # <- note extra ','
processThread.start()
print 'the file is run in the background'
关于Python,脚本结束时关闭具有不同SID的子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45404492/