本文介绍了与主服务器分离的subprocess.popen(Linux)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打开一个子进程,但是将其与调用它的父脚本分离.现在,如果我调用subprocess.popen,并且父脚本也使子进程模具崩溃.

I am trying to open a subprocess but have it be detached from the parent script that called it. Right now if I call subprocess.popen and the parent script crashes the subprocess dies as well.

我知道Windows有两个选项,但是* nix找不到任何东西.

I know there are a couple of options for windows but I have not found anything for *nix.

我也不需要使用子过程来调用它.我需要做的是能够校准另一个已分离的进程并获取pid.

I also don't need to call this using subprocess. All I need is to be able to cal another process detached and get the pid.

推荐答案

对于linux,这根本没有问题.只是Popen().例如,这里有一个dying_demon.py

With linux, it's no issue at all. Just Popen(). For example, here is a little dying_demon.py

#!/usr/bin/python -u
from time import sleep
from subprocess import Popen
print Popen(["python", "-u", "child.py"]).pid
i = 0
while True:
    i += 1
    print "demon: %d" % i
    sleep(1)
    if i == 3:
        i = hurz # exception

剥离child.py

#!/usr/bin/python -u
from time import sleep
i = 0
while True:
    i += 1
    print "child: %d" % i
    sleep(1)
    if i == 20:
        break

孩子继续计数(到控制台),而恶魔因异常死亡.

The child continues to count (to the console), while the demon is dying by exception.

这篇关于与主服务器分离的subprocess.popen(Linux)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 09:10