【问题标题】:Multiprocessing and dict empty after update [python3]更新后多处理和字典为空[python3]
【发布时间】:2018-03-11 10:32:01
【问题描述】:

我在 python3 中的代码有一个问题: 我尝试了许多解决方案,但结果仍然相同。

import pprint
import concurrent
import multiprocessing
from concurrent import futures

class exempleStackOverFlow: 
    def __init__(self):
        self.dict1={}
        self.headers = {"header1","header2","header3"}

    def fillIn(self,key):
        response = {key:{'first':['1','2','3']}}
        self.dict1.update(response)
        pprint.pprint(response)

e =  exempleStackOverFlow()

def try_my_operation(item):
    try:
        e.fillIn(item)      
    except:
        print('error with item')

executor = concurrent.futures.ProcessPoolExecutor(2)
futures = [executor.submit(try_my_operation, item) for item in e.headers]
concurrent.futures.wait(futures)
print(e.dict1)

结果

{'header3': {'first': ['1', '2', '3']}}
{'header1': {'first': ['1', '2', '3']}}
{'header2': {'first': ['1', '2', '3']}}
{}

预期

 {'header3': {'first': ['1', '2', '3']}}
 {'header1': {'first': ['1', '2', '3']}}
 {'header2': {'first': ['1', '2', '3']}}
 {'header1': {'first': ['1', '2', '3']},
'header2': {'first': ['1', '2', '3']},
     'header3': {'first': ['1', '2', '3']}}

(或任何标题的顺序)

我可能不明白这是如何工作的,我想了解一下我的问题,当我输入时,dict1 最后是空的 try_my_operation.

在没有多处理/并发的情况下它确实可以很好地更新(就像我这样做 e.fillIn("headers1"),e.fillIn("headers2")... 并且在我可以让 dict1 充满之后。

感谢您以后的任何 cmets

【问题讨论】:

  • 您正在一个主进程之上创建 2 个进程。您的结果是您的代码所期望的。得到你当前的结果加上打印所有三个从self.dict1.update 附加在一起的想法?
  • 感谢您的回答,这只是打印到跟踪更改的结果。在结果中,我返回的 dict 为空 {} 而不是我在“预期”中想要的,这意味着 self.dict1 equal to {'header1': {'first': ['1', '2', '3']}, 'header2': {'first': ['1', '2', '3']}, 'header3': {'first': ['1', '2', '3']}} 而不是 self.dict1 等于 {}
  • 在下面查看我的答案

标签: python-3.x function dictionary concurrency python-multiprocessing


【解决方案1】:

我相信您正在寻找 ThreadPoolExecutor,而不是 ProcessPoolExecutor。

这里我使用多线程(无并发)来获得您的预期输出:

import pprint
from threading import Thread

class exempleStackOverFlow: 
    def __init__(self):
        self.dict1={}
        self.headers = ["header1","header2","header3"]

    def fillIn(self,key):
        response = {key:{'first':['1','2','3']}}
        self.dict1.update(response)
        pprint.pprint(response)

def try_my_operation(item):
    try:
        e.fillIn(item)
    except:
        print('error with item')

e =  exempleStackOverFlow()

for i in range(len(e.headers)):
    futures = Thread(target = try_my_operation, args = (e.headers[i],));
    futures.start();
    futures.join();

print(e.dict1)

不同之处在于多处理中的e 不是共享对象,因此e 不会在所有进程之间更新。在多线程中,它们共享相同的变量,因此e 可以按预期更新。

编辑:

我不熟悉并发,但我确信它有一种类似的方法来共享类型和对象,如 multiprocessing does 显示 here 和使用像 here 这样的管理器的对象。

如果您正在寻找速度,那么一定要走多处理路线,并使用像 from multiprocessing.managers import BaseManager 这样的管理器来共享您的对象。

【讨论】:

  • 再次您好,感谢您的回答,它现在可以工作了,但是我失去了以前方法的所有速度?有什么猜测吗?
  • 是的。线程不提供 python 中的速度改进。请参阅我的答案的编辑。
  • 是的,速度是唯一的目的,好的,我会看看 BaseManager !我很快就会回来回答这篇文章。感谢您的宝贵时间
【解决方案2】:

在@Treyten Care 的帮助下

需要添加到我的初始帖子中:

from multiprocessing import Process, Manager 

class exempleStackOverFlow: 
        def __init__(self):
            manager = Manager()
            self.dict1 = manager.dict()

有了这个我可以更新我的字典。 谢谢

【讨论】:

    猜你喜欢
    • 2020-02-20
    • 2020-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 2020-09-30
    • 1970-01-01
    相关资源
    最近更新 更多