作者wheado (principal component QQ)
看板Python
标题[问题] Multiprocess Process 回传问题(解决)
时间Wed Nov 3 14:36:48 2021
我有两个函数想要使用 python 中的 process 方法同时进行,
测试代码已经写好如下,基本上可以执行,
将这两个函数的执行结果进行打印。
原先问题是希望回传 list 并且其中元素是 dataframe 形式,
已经自行解决,代码如下。
## ------------------------------------------
from multiprocessing import Process, Manager
import pandas
import time
def loop_a(ls, output):
for i in ls:
print(i)
time.sleep(1)
pass
output['a'] = pandas.Dataframe({'a':['finish a']})
return
def loop_b(ls, output):
for i in ls:
print(i)
time.sleep(0.5)
pass
output['b'] = pandas.Dataframe({'b':['finish b']})
return
manager = Manager()
output = manager.dict()
task_a = Process(target=loop_a, args=[[1,2,3,4,5], output])
task_b = Process(target=loop_b, args=[[6,7,8], output])
task_a.start()
task_b.start()
output.values()
## ---------------------------------------------------
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 111.254.34.165 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1635921410.A.D47.html
※ 编辑: wheado (111.254.34.165 台湾), 11/03/2021 15:09:42
※ 编辑: wheado (111.254.34.165 台湾), 11/03/2021 15:15:27