python - imap 无序值访问
问题描述
我有以下情况,说明了一个更复杂的情况。
依赖项:
#formula
def cube_rand(x):
c = x**3
time.sleep(random.random())
return (x,c)
#dataframe to be used
dict_input = {
'col1': ['a','b','c'],
'col2': [1,2,3]
}
df_test = pd.DataFrame(dict_input)
imap 实例:
from multiprocessing import Pool
start = time.time()
p=Pool(8)
results = p.imap_unordered(cube_rand, (index for index,row in df_test.iterrows()))
print(results)
done = time.time()
print("processing time: ", done-start)
我想为定义的数据框中的每一行打印无序的对 ("index","index^3")。但是,我收到以下输出:
<multiprocessing.pool.IMapUnorderedIterator object at 0x7fb5af0cbd50>
processing time: 0.15177679061889648
关于如何解决这个问题的任何猜测?
谢谢
解决方案
您需要迭代imap_unordered
调用的结果:
for x in p.imap_unordered(cube_rand, (index for index,row in df_test.iterrows())):
print(x)
输出:
(0, 0)
(2, 8)
(1, 1)
processing time: 1.1946678161621094
推荐阅读
- c# - 在泛型方法中使用类型为 Class 的 switch
- php - WordPress:如何使用 PHP 和 MySQL 更新数据库中的数据?
- sql - 将结果汇总到一个联合中
- java - 如何向 Dataframe 添加一些信息?
- entity-framework - 设计 ASP.NET Core Web API 的最佳实践是什么
- dart - 仅在设备上诊断 Flutter MaterialApp 主题运行时更改缓慢的原因
- vbscript - vbscript AscB 中的过程调用或参数无效
- javascript - 如何从数组中删除空格?
- python - 组合表达式中的运算
- firebase - 带有 Firestore 的 StreamController