使用python多处理库让父进程在子进程之前返回 [英] Let parent process return before child process using python multiprocessing library

查看:56
本文介绍了使用python多处理库让父进程在子进程之前返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当从 python 创建具有多处理库的进程时,父进程在返回之前等待其子进程返回.事实上,文档建议加入所有孩子.但我想让父进程在其子进程完成之前返回.

When one creates Processes with multiprocessing library from python, the parent process waits for its children to return before it returns. In fact, the documentation recommends joining all children. But I would like to let the parent return before its child process finish.

有没有办法分离"子进程.

Is there any way to "detach" the child process.

我知道使用 subprocess.Popen 可以创建分离的子进程,但我想使用多处理库中的功能,如队列、锁等.

I know that using subprocess.Popen it is possible to create detached child processes, but i would like to use the features from multiprocessing library, like Queues, Locks and so.

我做了两个例子来说明区别.

I made two examples to show the difference.

第一个示例使用多处理库.当这个脚本被调用时,它打印父消息,等待 5 秒,打印子消息,然后才返回.

The first example uses the multiprocessing library. When this script is called, it prints the parent message, waits 5 seconds, prints the child message and only then return.

# Using multiprocessing, only returns after 5 seconds
from multiprocessing import Process
from time import sleep, asctime

def child():
    sleep(5.0)
    print 'Child end reached on', asctime()

if __name__ == '__main__':
    p = Process(target = child)
    p.start()
    # Detach child process here so parent can return.
    print 'Parent end reached on', asctime()

第二个示例使用 subprocess.Popen.调用此脚本时,它会打印父消息,返回 (!!!) 并在 5 秒后打印子消息.

The second example uses the subprocess.Popen. When this script is called, it prints the parent message, returns (!!!) and after 5 seconds prints the child message.

# Using Popen, returns immediately.
import sys
from subprocess import Popen
from time import sleep, asctime

def child():
    sleep(5)
    print 'Child end reached on', asctime()

if __name__ == '__main__':
    if 'call_child' in sys.argv:
        child()
    else:
        Popen([sys.executable] + [__file__] + ['call_child'])
        print 'Parent end reached on', asctime()

如果我可以传递队列、管道、锁、信号量等,第二个例子是可以接受的.

The second example would be acceptable if I could pass Queues, Pipes, Locks, Semaphores, etc..

IMO,第一个示例也导致代码更清晰.

IMO, the first example also leads to a more cleaner code.

我在 Windows 上使用 python 2.7.

I'm using python 2.7 on Windows.

推荐答案

只要从当前进程对象的_children集合中移除进程对象,父进程就会立即退出.

Just remove the process object from the _children set of the current process object, the the parent process will exit immediately.

多处理模块管理私有集合中的子进程,并在当前进程退出时加入它们.如果您不关心孩子,您可以将其从集合中删除.

Theh multiprocessing module manages child processes in a private set and join them when the current process exits. You can remove children from the set if you don't care of them.

process = multiprocessing.Process(target=proc_main)
multiprocessing.current_process()._children.discard(process)
exit(0)

这篇关于使用python多处理库让父进程在子进程之前返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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