分离使用python多处理模块启动的子进程

分离使用python多处理模块启动的子进程,python,subprocess,multiprocessing,detach,pyro,Python,Subprocess,Multiprocessing,Detach,Pyro,我想使用python中的mutliprocessing模块创建一个进程,但要确保它在创建子进程的进程退出后继续运行 我可以使用subprocess模块和Popen获得所需的功能,但我希望将代码作为函数而不是脚本运行。我这样做的原因是为了简化pyro(python远程对象)对象的创建。我想使用多处理在一个单独的进程中启动pyro对象请求处理程序,但我想在支持pyro对象的进程继续运行时退出主进程。我终于得到了我想要的。我很感激任何改进代码的建议 def start_server(): py

我想使用python中的mutliprocessing模块创建一个进程,但要确保它在创建子进程的进程退出后继续运行


我可以使用subprocess模块和Popen获得所需的功能,但我希望将代码作为函数而不是脚本运行。我这样做的原因是为了简化pyro(python远程对象)对象的创建。我想使用多处理在一个单独的进程中启动pyro对象请求处理程序,但我想在支持pyro对象的进程继续运行时退出主进程。

我终于得到了我想要的。我很感激任何改进代码的建议

def start_server():
    pyrodaemon = Pyro.core.Daemon()
    #setup daemon and nameserver
    #Don't want to close the pyro socket
    #Need to remove SIGTERM map so Processing doesn't kill the subprocess
    #Need to explicitly detach for some reason I don't understand
    with daemon.DaemonContext(files_preserve=[pyrodaemon.sock],signal_map={signal.SIGTERM:None},detach_process=True):
        while running:
            pyrodaemon.handleRequests(timeout=1.0)
    #when finished, clean up
    pyrodaemon.shutdown()

def main():
    p = Process(target=start_server)
    p.daemon=True # Need to inform Process that this should run as a daemon
    p.start()
    time.sleep(3.0) # Important when running this program stand alone: Must wait long enough for start_server to get into the daemon context before the main program exits or Process will take down the subprocess before it detaches
    do_other_stuff_not_in_the_daemon()

守护进程这个词在这里被滥用;)不要将Process.daemon设置为True。这会告诉多处理尝试在退出时杀死子进程(混淆了吗?)。我认为这就是为什么您需要在上面的代码中捕获SIGTERM并设置detach_进程的原因。-下面是另一种分离使用
多处理启动的进程的方法