您的问题是使用
subprocess.check_output-是正确的,您无法使用该接口获取子PID。改用Popen:
proc = subprocess.Popen(["ls", "-l"], stdout=PIPE, stderr=PIPE)# Here you can get the PIDglobal child_pidchild_pid = proc.pid# Now we can wait for the child to complete(output, error) = proc.communicate()if error: print "error:", errorprint "output:", output
为确保您在出口处杀死孩子:
import osimport signaldef kill_child(): if child_pid is None: pass else: os.kill(child_pid, signal.SIGTERM)import atexitatexit.register(kill_child)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)