基于对此问题的接受答案:python-subprocess-callback-when-cmd-exits我在一个单独的线程中运行子进程,并在子进程完成后执行可调用。一切都很好,但问题是即使将线程作为守护进程运行,即使程序正常退出或被kill -9
,Ctrl + C等等杀死,子进程仍继续运行...
下面是一个非常简化的例子(在2.7上运行):
import threading
import subprocess
import time
import sys
def on_exit(pid):
print 'Process with pid %s ended' % pid
def popen_with_callback(cmd):
def run_in_thread(command):
proc = subprocess.Popen(
command,
shell=False
)
proc.wait()
on_exit(proc.pid)
return
thread = threading.Thread(target=run_in_thread, args=([cmd]))
thread.daemon = True
thread.start()
return thread
if __name__ == '__main__':
popen_with_callback(
[
"bash",
"-c",
"for ((i=0;i<%s;i=i+1)); do echo $i; sleep 1; done" % sys.argv[1]
])
time.sleep(5)
print 'program ended'
如果主线程比子进程持续时间更长,那么一切都很好:
(venv)~/Desktop|➤➤ python testing_threads.py 3
> 0
> 1
> 2
> Process with pid 26303 ended
> program ended
如果主线程持续的时间少于子进程,则子进程继续运行,直到它最终挂起:
(venv)~/Desktop|➤➤ python testing_threads.py 8
> 0
> 1
> 2
> 3
> 4
> program ended
(venv)~/Desktop|➤➤ 5
> 6
> 7
# hanging from now on
如果主程序完成或终止,如何终止子进程?我试图在atexit.register(os.kill(proc.pid, signal.SIGTERM))
之前使用proc.wait
但它实际上在运行子进程的线程退出时执行,而不是在 main 线程退出时执行。
我还在考虑对父pid进行轮询,但由于proc.wait
情况,我不确定如何实现它。
理想的结果将是:
(venv)~/Desktop|➤➤ python testing_threads.py 8
> 0
> 1
> 2
> 3
> 4
> program ended
> Process with pid 1234 ended
答案 0 :(得分:0)
使用Thread.join
方法阻止主线程,直到该线程退出:
if __name__ == '__main__':
popen_with_callback(
[
"bash",
"-c",
"for ((i=0;i<%s;i=i+1)); do echo $i; sleep 1; done" % sys.argv[1]
]).join()
print 'program ended'