与常规 dict 相比,Python manager.dict() 非常慢

2023-03-12Python开发问题
17

本文介绍了与常规 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() 非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10

按10分钟间隔对 pandas 数据帧进行分组
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)...
2024-08-22 Python开发问题
11