故意在python中创建一个孤立的进程 [英] Deliberately make an orphan process in python

查看:88
本文介绍了故意在python中创建一个孤立的进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为MyScript的python脚本(类似于基于RHEL的unix),具有两个函数,分别称为A和B.我希望它们在不同的独立进程中运行(分离B和A):

I have a python script (unix-like, based on RHEL), called MyScript, that has two functions, called A and B. I'd like them to run in different, independent processes (detach B and A):

  • 启动脚本MyScript
  • 执行功能A
  • 产生一个新进程,将数据从函数A传递到B
  • 功能B运行时,继续功能A
  • 函数A完成后,即使B仍在运行,也退出MyScript

我认为我应该使用多重处理来创建守护进程,但是文档表示这不是正确的用例.因此,我决定生成一个子进程和一个child ^ 2进程(该子进程),然后强制该子进程终止.尽管此解决方法似乎可行,但看起来确实很丑陋.

I thought I should use multiprocessing to create a daemon process, but the documentation suggests that's not the right usecase. So, I decided to spawn a child process and child^2 process (the child's child), and then force the child to terminate. While this workaround appears to work, it seems really ugly.

您能帮我把它变得更pythonic吗?子流程模块是否具有可以在函数上运行的方法?下面的示例代码.

Can you help me make it more pythonic? Does the subprocess module have a method that will operate on a function? Sample code below.

import multiprocessing
import time
import sys
import os

def parent_child():
    p = multiprocessing.current_process()
    print 'Starting parent child:', p.name, p.pid
    sys.stdout.flush()
    cc = multiprocessing.Process(name='childchild', target=child_child)
    cc.daemon = False
    cc.start()
    print 'Exiting parent child:', p.name, p.pid
    sys.stdout.flush()

def child_child():
    p = multiprocessing.current_process()
    print 'Starting child child:', p.name, p.pid
    sys.stdout.flush()
    time.sleep(30)
    print 'Exiting child child:', p.name, p.pid
    sys.stdout.flush()

def main():
    print 'starting main', os.getpid()
    d = multiprocessing.Process(name='parentchild', target=parent_child)
    d.daemon = False
    d.start()
    time.sleep(5)
    d.terminate()
    print 'exiting main', os.getpid()

main()

推荐答案

这里只是原始代码的随机版本,它将功能转移到单个调用spawn_detached(callable)中.即使程序退出后,它也可以保持分离的进程运行:

Here is just a random version of your original code that moves the functionality into a single call spawn_detached(callable). It keeps the detached process running even after the program exits:

import time
import os
from multiprocessing import Process, current_process

def spawn_detached(callable):
    p = _spawn_detached(0, callable)
    # give the process a moment to set up
    # and then kill the first child to detach
    # the second.
    time.sleep(.001)
    p.terminate()

def _spawn_detached(count, callable):
    count += 1
    p = current_process()
    print 'Process #%d: %s (%d)' % (count, p.name, p.pid)

    if count < 2:
        name = 'child'
    elif count == 2:
        name = callable.func_name
    else:
        # we should now be inside of our detached process
        # so just call the function
        return callable()

    # otherwise, spawn another process, passing the counter as well
    p = Process(name=name, target=_spawn_detached, args=(count, callable)) 
    p.daemon = False 
    p.start()
    return p

def operation():
    """ Just some arbitrary function """
    print "Entered detached process"
    time.sleep(15)
    print "Exiting detached process"


if __name__ == "__main__":
    print 'starting main', os.getpid()
    p = spawn_detached(operation)
    print 'exiting main', os.getpid()

这篇关于故意在python中创建一个孤立的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆