Python manager.dict() is very slow compared to regular dict(与常规 dict 相比,Python manager.dict() 非常慢)
问题描述
我有一个存储对象的字典:
I have a dict to store objects:
jobs = {}
job = Job()
jobs[job.name] = job
现在我想将它转换为使用 manager dict,因为我想使用多处理并且需要在进程中共享这个 dict
now I want to convert it to use manager dict because I want to use multiprocessing and need to share this dict amonst processes
mgr = multiprocessing.Manager()
jobs = mgr.dict()
job = Job()
jobs[job.name] = job
仅仅通过转换为使用 manager.dict() 事情变得非常缓慢.
just by converting to use manager.dict() things got extremely slow.
例如,如果使用原生 dict,创建 625 个对象并将其存储到 dict 中只需要 0.65 秒.
For example, if using native dict, it only took .65 seconds to create 625 objects and store it into the dict.
同样的任务现在需要 126 秒!
The very same task now takes 126 seconds!
我可以做任何优化以使 manager.dict() 与 python {} 保持一致?
Any optimization i can do to keep manager.dict() on par with python {}?
推荐答案
问题是由于某种原因每次插入都很慢(在我的机器上慢了 117 倍),但是如果你更新你的 manager.dict()
使用普通的dict,这将是一个快速的操作.
The problem is that each insert is quite slow for some reason (117x slower on my machine), but if you update your manager.dict()
with a normal dict, it will be a single and fast operation.
jobs = {}
job = Job()
jobs[job.name] = job
# insert other jobs in the normal dictionary
mgr = multiprocessing.Manager()
mgr_jobs = mgr.dict()
mgr_jobs.update(jobs)
然后使用 mgr_jobs
变量.
另一种选择是使用广泛采用的 multiprocessing.Queue
类.
Another option is to use the widely adopted multiprocessing.Queue
class.
这篇关于与常规 dict 相比,Python manager.dict() 非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:与常规 dict 相比,Python manager.dict() 非常慢


- YouTube API v3 返回截断的观看记录 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01