python - python中的共享内存并行处理

标签 python multithreading python-2.7 python-multiprocessing

我有一个字符串列表,我想将这些列表元素放入适当的存储桶中。

def bucketElements(i):
    global buckA, buckB, buckC
    if i.startswith('A'):
        buckA.add(i)
    elif i.startswith('B'):
        buckB.add(i)
    elif i.startswith('C'):
        buckC.add(i)

现在我想为列表中的每个元素并行调用此方法。像这样的事情,

buckA,buckB, buckC = set(), set(), set()
pool = multiprocessing.Pool(processes=10)
pool.map(bucketElements, buckList)

由于我正在更新函数内的全局变量,因此我无法使用多重处理。无论如何我可以改进我的处理吗?目前我这样调用它,

buckA,buckB, buckC = set(), set(), set()
for i in buckList:
    bucketElements(i)

最佳答案

您有 3 个选择:

  • 队列()
  • Manager.Value()
  • Manager.list()

Python » 文档:multiprocessing.html#managers

关于python - python中的共享内存并行处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44699677/

相关文章:

java - 使用线程填充 JList

python-2.7 - NumPy 的 : Translating elements increases size of file by a lot (factor of 8)

Python 速度差异

python - 使用 Python 从 json API 响应中提取属性

java - 从性能上来说,线程(比如swing线程) "interfere"和我的线程有关系吗?

java - 在java中同步大量线程的最佳方法

python - 使用 ListView 计算对象

python - Django-Piston 中的错误处理

python - 读取部分文件,以特定单词停止和开始

python - 使用我自己的语料库在 Python NLTK 中进行类别分类