Python 多处理 imap [英] Python Multiprocessing imap

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

问题描述

今天我又有一个关于多处理的问题...我有一个小的示例代码:

today i have again a question about multiprocessing... I have a small example code:

import multiprocessing

def function(a):
    v = a**2
    w = a**3
    x = a**4
    y = a**5
    z = a**6


b = [1,2,3,4,5,6,7,8,9,10]

if __name__ == "__main__":
    pool = multiprocessing.Pool(processes=4)
    pool.imap(function, b)

结果应该是 5 个列表(vlist、wlist、xlist、ylist 和 zlist).vlist 应该包含所有 v 结果,wlist 应该包含 w 结果等等.我想对所有结果进行排序,所以我想使用 imap,我希望它是正确的方法.我以为我可以使用诸如附加命令之类的东西,但我不知道如何...

The results should be 5 lists (vlist, wlist, xlist, ylist and zlist). The vlist should have all the v results, the wlist the w results and so on. I want to get all results ordered, so i want to use imap, i hope its the right way. I thought i could use something like append commands but i don't know how...

感谢您的帮助,约翰

推荐答案

imap() 或任何映射函数接受一个值并通过函数的返回值将其映射"到另一个值.在您发布的代码段中, function() 不返回任何内容.

imap() or any mapping function takes one value and 'maps' it to another via the function's return value. In the snippet you posted, function() doesn't return anything.

import multiprocessing

def function(a):
    v = a**2
    w = a**3
    x = a**4
    y = a**5
    z = a**6
    return v, w, x, y, z

b = [1,2,3,4,5,6,7,8,9,10]

pool = multiprocessing.Pool(processes=4)
vals = pool.imap(function, b)

for i in vals:
    print i

输出为:

(1, 1, 1, 1, 1)
(4, 8, 16, 32, 64)
(9, 27, 81, 243, 729)
(16, 64, 256, 1024, 4096)
(25, 125, 625, 3125, 15625)
(36, 216, 1296, 7776, 46656)
(49, 343, 2401, 16807, 117649)
(64, 512, 4096, 32768, 262144)
(81, 729, 6561, 59049, 531441)
(100, 1000, 10000, 100000, 1000000)

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

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