您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

父母去世时如何杀死用subprocess.check_output()创建的python子进程?

父母去世时如何杀死用subprocess.check_output()创建的python子进程?

您的问题是使用subprocess.check_output-是正确的,您无法使用该接口获取子PID。改用Popen:

proc = subprocess.Popen(["ls", "-l"], stdout=PIPE, stderr=PIPE)

# Here you can get the PID
global child_pid
child_pid = proc.pid

# Now we can wait for the child to complete
(output, error) = proc.communicate()

if error:
    print "error:", error

print "output:", output

为了确保您在出口处杀死孩子:

import os
import signal
def kill_child():
    if child_pid is None:
        pass
    else:
        os.kill(child_pid, signal.SIGTERM)

import atexit
atexit.register(kill_child)
python 2022/1/1 18:32:57 有219人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶