栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python多重处理-跟踪pool.map操作的过程

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python多重处理-跟踪pool.map操作的过程

如果使用迭代

map
函数,则跟踪进度非常容易。

>>> from pathos.multiprocessing import ProcessingPool as Pool>>> def simFunction(x,y):...   import time...   time.sleep(2)...   return x**2 + y... >>> x,y = range(100),range(-100,100,2)>>> res = Pool().imap(simFunction, x,y)>>> with open('results.txt', 'w') as out:...   for i in x:...     out.write("%sn" % res.next())...     if i%10 is 0:...       print "%s of %s simulated" % (i, len(x))... 0 of 100 simulated10 of 100 simulated20 of 100 simulated30 of 100 simulated40 of 100 simulated50 of 100 simulated60 of 100 simulated70 of 100 simulated80 of 100 simulated90 of 100 simulated

或者,您可以使用异步

map
。在这里,我将做些不同的事情,只是将其混合在一起。

>>> import time>>> res = Pool().amap(simFunction, x,y)>>> while not res.ready():...   print "waiting..."...   time.sleep(5)... waiting...waiting...waiting...waiting...>>> res.get()[-100, -97, -92, -85, -76, -65, -52, -37, -20, -1, 20, 43, 68, 95, 124, 155, 188, 223, 260, 299, 340, 383, 428, 475, 524, 575, 628, 683, 740, 799, 860, 923, 988, 1055, 1124, 1195, 1268, 1343, 1420, 1499, 1580, 1663, 1748, 1835, 1924, 2015, 2108, 2203, 2300, 2399, 2500, 2603, 2708, 2815, 2924, 3035, 3148, 3263, 3380, 3499, 3620, 3743, 3868, 3995, 4124, 4255, 4388, 4523, 4660, 4799, 4940, 5083, 5228, 5375, 5524, 5675, 5828, 5983, 6140, 6299, 6460, 6623, 6788, 6955, 7124, 7295, 7468, 7643, 7820, 7999, 8180, 8363, 8548, 8735, 8924, 9115, 9308, 9503, 9700, 9899]

请注意,我使用

pathos.multiprocessing
而不是
multiprocessing
。这仅仅是一个分支
multiprocessing
,使您能够
map
使用多个输入来执行函数,具有更好的序列化,并允许您在
map
任何地方(不仅是in
__main__
)执行调用。您也可以使用
multiprocessing
上面的方法,但是代码会稍有不同。

迭代的或异步的

map
都可以使您编写任何代码,以进行更好的过程跟踪。例如,将唯一的“
id”传递给每个作业,并观察返回的作业,或者让每个作业返回其进程ID。有很多跟踪进度和过程的方法……但是以上内容应该为您提供一个开始。

你可以在

pathos
这里找到:https :
//github.com/uqfoundation



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/638746.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号