多处理和变量返回? [英] Multiprocessing and variable return?

查看:72
本文介绍了多处理和变量返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天大部分时间都在反对Python中的多处理,我取得的进展很小-我很抱歉,如果我的问题是重复的或者我的无知很明显-我不能找不到以这种方式问到其他地方.

I have been banging my head against Multiprocessing in Python for the better part of the day now, and I've managed to make very little progress - I apologize if my question is a duplicate or my ignorance is apparent - I couldn't find it asked anywhere else in this way.

我正在寻找一种并行运行函数的方法,并将函数产生的任意结果返回给主脚本.

I'm looking for a way to run functions in parallel, and return some arbitrary thing they've produced back to the main script.

问题是:从多重处理启动的Process()可以返回列表或其他任意变量类型吗?

The question is: Can a Process() started from Multiprocessing return a list or some other arbitrary variable type?

例如,我想:

def 30_second_function():
    #pretend this takes 30 seconds to run
    return ["mango", "habanero", "salsa"]
#End 30_second_function()

def 5_second_function():
    #pretend this takes 5 seconds to run
    return {"beans": "8 oz", "tomato paste": "16 oz"}
#End 5_second_function()

p1 = multiprocessing.Process(target=30_second_function)
p1.start()
p2 = multiprocessing.Process(target=5_second_function)
p2.start()

#Somehow retrieve the list and the dictionary here.  p1.returned??

然后以某种方式从30_second_function访问列表,并从5_second_function访问字典.这可能吗?我会用这种错误的方式吗?

And then somehow access the list from 30_second_function and the dictionary from 5_second_function. Is this possible? Am I going about this the wrong way?

推荐答案

Process本身不提供获取返回值的方法. 要在进程之间交换数据,您需要使用队列,管道,共享内存,...:

Process itself does not provide a way to get return value. To exchange data between processes, you need to use queue, pipe, shared memory, ...:

import multiprocessing

def thirty_second_function(q):
    q.put(["mango", "habanero", "salsa"])

def five_second_function(q):
    q.put({"beans": "8 oz", "tomato paste": "16 oz"})

if __name__ == '__main__':
    q1 = multiprocessing.Queue()
    p1 = multiprocessing.Process(target=thirty_second_function, args=(q1,))
    p1.start()

    q2 = multiprocessing.Queue()
    p2 = multiprocessing.Process(target=five_second_function, args=(q2,))
    p2.start()

    print(q1.get())
    print(q2.get())

替代使用 multiprocessing.pool.Pool :

import multiprocessing.pool

def thirty_second_function():
    return ["mango", "habanero", "salsa"]

def five_second_function():
    return {"beans": "8 oz", "tomato paste": "16 oz"}

if __name__ == '__main__':
    p = multiprocessing.pool.Pool()
    p1 = p.apply_async(thirty_second_function)
    p2 = p.apply_async(five_second_function)

    print(p1.get())
    print(p2.get())

或使用 concurrent.futures模块(

这篇关于多处理和变量返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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