我正在尝试理解python中的多处理。
from multiprocessing import Process
def multiply(a,b):
print(a*b)
return a*b
if __name__ == '__main__':
p = Process(target= multiply, args= (5,4))
p.start()
p.join()
print("ok.")
在此代码块中,例如,如果有一个名为“result”的变量。我们如何将乘法函数的返回值赋给“结果”?
关于IDLE的一个小问题:当我试图用Python Shell运行这个示例时,它无法正常工作?如果我双击.py文件,输出就是:
20
ok.
但是,如果我尝试在IDLE中运行它:
ok.
...谢谢
答案 0 :(得分:13)
好的,我以某种方式管理了这个。我查看了python文档,并了解到:使用Queue类,我们可以从函数中获取返回值。我的代码的最终版本是这样的:
from multiprocessing import Process, Queue
def multiply(a,b,que): #add a argument to function for assigning a queue
que.put(a*b) #we're putting return value into queue
if __name__ == '__main__':
queue1 = Queue() #create a queue object
p = Process(target= multiply, args= (5,4,queue1)) #we're setting 3rd argument to queue1
p.start()
print(queue1.get()) #and we're getting return value: 20
p.join()
print("ok.")
还有一个pipe()函数,我想我们也可以使用管道函数。但是队列现在为我工作了。
答案 1 :(得分:5)
这有帮助吗?这需要一个函数列表(及其参数),并行运行它们, 并返回他们的输出:(这是旧的。更新版本是https://gitlab.com/cpbl/cpblUtilities/blob/master/parallel.py)
def runFunctionsInParallel(listOf_FuncAndArgLists):
"""
Take a list of lists like [function, arg1, arg2, ...]. Run those functions in parallel, wait for them all to finish, and return the list of their return values, in order.
(This still needs error handling ie to ensure everything returned okay.)
"""
from multiprocessing import Process, Queue
def storeOutputFFF(fff,theArgs,que): #add a argument to function for assigning a queue
print 'MULTIPROCESSING: Launching %s in parallel '%fff.func_name
que.put(fff(*theArgs)) #we're putting return value into queue
queues=[Queue() for fff in listOf_FuncAndArgLists] #create a queue object for each function
jobs = [Process(target=storeOutputFFF,args=[funcArgs[0],funcArgs[1:],queues[iii]]) for iii,funcArgs in enumerate(listOf_FuncAndArgLists)]
for job in jobs: job.start() # Launch them all
for job in jobs: job.join() # Wait for them all to finish
# And now, collect all the outputs:
return([queue.get() for queue in queues])