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() 非常慢


基础教程推荐
- 用于分类数据的跳跃记号标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 筛选NumPy数组 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01