使用多处理附加到同一个列表 - python [英] append to the same list with multiprocessing - python

查看:25
本文介绍了使用多处理附加到同一个列表 - python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将不同的进程附加到同一个列表中:

I'd like different processes to append to the same list:

import multiprocessing as mp

def foo(n,L):
    a
    L.append(n)

pool = mp.Pool(processes=2)
manager = mp.Manager()

L= manager.list()

l=[[1,2],[3,4],[5,6],[7,8]]

[pool.apply_async(foo, args=[n,L]) for n in l]

然而,

>> print L
[]

我做错了什么?

问题是我的原始代码中有回溯,但没有打印在屏幕上.例如,如果我运行上面的代码并且 a 不存在,则不会评估其余代码,但不会引发异常.有没有办法进行多处理打印回溯,以便我可以调试代码?

The problem was that there was traceback in my original code which wasn't printed on the screen. For example if I run the code above and a doesn't exist, the rest of the code isn't evaluated, but there is no exception raised. Is there a way to make multiprocessing print tracebacks so that I can debug the code?

推荐答案

因为您还没有等待任务完成.

Because you haven't waited tasks to finish.

import multiprocessing as mp

def foo(n,L):
    L.append(n)

pool = mp.Pool(processes=2)
manager = mp.Manager()

L= manager.list()

l=[[1,2],[3,4],[5,6],[7,8]]

[pool.apply_async(foo, args=[n,L]) for n in l]
pool.close()
pool.join()
print(L)

这篇关于使用多处理附加到同一个列表 - python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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