我有一个存储对象的字典:
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() 非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何将函数绑定到 Qt 菜单栏中的操作?How to bind a function to an Action from Qt menubar?(如何将函数绑定到 Qt 菜单栏中的操作?)
PyQt 启动后进度跃升至 100%PyQt progress jumps to 100% after it starts(PyQt 启动后进度跃升至 100%)
如何将 yaxis 刻度标签设置在固定位置,以便当我How to set yaxis tick label in a fixed position so that when i scroll left or right the yaxis tick label should be visible?(如何将 yaxis 刻度标签设
`QImage` 构造函数有未知关键字 `data``QImage` constructor has unknown keyword `data`(`QImage` 构造函数有未知关键字 `data`)
将 x 轴刻度更改为自定义字符串Change x-axis ticks to custom strings(将 x 轴刻度更改为自定义字符串)
如何在python中将文件保存为excel时显示进度条?How to show progress bar while saving file to excel in python?(如何在python中将文件保存为excel时显示进度条?)