python - 使用多处理时python中的缓慢pickle转储
问题描述
所以,我正在尝试使用多处理模块并行化一个使用 python 3.7 解决 pyomo 实例的函数。该代码有效,但启动时间很荒谬(每个进程约 25 秒)。奇怪的是,我在另一台计算机上尝试了相同的代码,但功能却没有那么强大,它下降到大约 2 秒(相同的代码,相同数量的并行进程,除了 Python 之外的所有东西的相同版本,在那台电脑上是 3.6)。
使用cProfile,我发现pickler 的dump 方法是耗时的方法,但我似乎不明白为什么要花这么长时间。数据很小,我通过使用 sys.getsizeof() 检查并行化函数的任何参数是否大于预期,但事实并非如此。
有谁知道泡菜转储缓慢的原因是什么?
编码:
from pyomo.environ import *
from pyomo.opt import SolverFactory, TerminationCondition
from pyomo.opt.parallel import SolverManagerFactory
import sys
import multiprocessing
def worker(init_nodes[i_nodo][j_nodo], data, optsolver, queue, shared_incumbent_data):
#[pyomo instances solving and constraining]
return
def foo(model, data, optsolver, processes = multiprocessing.cpu_count()):
queue = multiprocessing.Queue()
process_dict = {}
for i_node in range(len(init_nodes)): #init_nodes is a list containing lists of pyomo instances
for j_node in range(len(init_nodes[i_node])):
process_name = str(i_node) + str(j_node)
print(" - Data size:", sys.getsizeof(data)) #same for all of the args
process_dict[process_name] = multiprocessing.Process(target=worker, args=(init_nodes[i_nodo][j_nodo], data, optsolver, queue, shared_incumbent_data))
pr = cProfile.Profile()
pr.enable()
process_dict[process_name].start()
pr.disable()
ps = pstats.Stats(pr)
ps.sort_stats('time').print_stats(5)
for n_nodo in process_dict:
process_dict[n_nodo].join(timeout=0)
#imports
#[model definition]
#[data is obtained from 3 .tab files, the biggest one has a 30 x 40 matrix, with 1 to 3 digit integers]
optsolver = SolverFactory("gurobi")
if __name__ == "__main__":
foo(model, data, optsolver, 4)
sys.getsizeof() 获得的参数大小和第一台计算机上的 .start() 配置文件
- Data size: 56
- Init_nodes size: 72
- Queue size: 56
- Shared incumbent data size: 56
7150 function calls (7139 primitive calls) in 25.275 seconds
Ordered by: internal time
List reduced from 184 to 5 due to restriction <5>
ncalls tottime percall cumtime percall filename:lineno(function)
2 25.262 12.631 25.267 12.634 {method 'dump' of '_pickle.Pickler' objects}
1 0.004 0.004 0.004 0.004 {built-in method _winapi.CreateProcess}
1265 0.002 0.000 0.004 0.000 C:\Users\OLab\AppData\Local\Continuum\anaconda3\lib\site-packages\pyomo\core\expr\numeric_expr.py:186(__getstate__)
2 0.001 0.001 0.002 0.001 <frozen importlib._bootstrap_external>:914(get_data)
1338 0.001 0.000 0.002 0.000 C:\Users\OLab\AppData\Local\Continuum\anaconda3\lib\site-packages\pyomo\core\expr\numvalue.py:545(__getstate__)
sys.getsizeof() 获得的参数大小和第二台计算机上的 .start() 配置文件
- Data size: 56
- Init_nodes size: 72
- Queue size: 56
- Shared incumbent data size: 56
7257 function calls (7247 primitive calls) in 1.742 seconds
Ordered by: internal time
List reduced from 184 to 5 due to restriction <5>
ncalls tottime percall cumtime percall filename:lineno(function)
2 1.722 0.861 1.730 0.865 {method 'dump' of '_pickle.Pickler' objects}
1 0.009 0.009 0.009 0.009 {built-in method _winapi.CreateProcess}
1265 0.002 0.000 0.005 0.000 C:\Users\Palbo\Anaconda2\envs\py3\lib\site-packages\pyomo\core\expr\numeric_expr.py:186(__getstate__)
1339 0.002 0.000 0.003 0.000 C:\Users\Palbo\Anaconda2\envs\py3\lib\site-packages\pyomo\core\expr\numvalue.py:545(__getstate__)
1523 0.001 0.000 0.001 0.000 {built-in method builtins.hasattr}
干杯!
第一台计算机的规格应该更快但不是:
- 适用于工作站的 Windows 10 专业版
- Intel Xeon Silver 4114 CPU @2.20 GHz 2.19 GHz(每个 10 核)
- 64 GB 内存
第二台电脑规格:
- 视窗 8.1
- Intel Core i3-2348M CPU @2.30 Ghz 2.30 Ghz(每个 2 核)
- 6 GB 内存
解决方案
最后找到了一个解决方案,将函数参数的酸洗转储到文件中,然后将文件名作为参数传递给 worker() 函数,然后在每个并行进程中从函数内打开每个文件。
转储时间从 ~24[s] 下降到 ~0.005[s]!
def worker(pickled_file_name, queue, shared_incumbent):
with open(pickled_file_name, "rb") as f:
data_tuple = pickle.load(f, encoding='bytes')
instance, data, optsolver, int_var_list, process_name, relaxed_incumbent = data_tuple
return
def foo():
[...]
picklefile = open("pickled_vars"+str(i_nodo)+str(j_nodo)+".p", "wb")
picklefile.write(pickle.dumps(variables_,-1))
picklefile.close()
process_dict[process_name] = multiprocessing.Process(target=bnbparallelbranching, args=("pickled_vars"+str(i_nodo)+str(j_nodo)+".p", q, shared_incumbent_data))
process_dict[process_name].start()
推荐阅读
- node.js - 尝试运行机器人时出现弃用/UnhandledPromiseRejection 警告
- asp.net-core - C# razor 应用程序中是否可以有多个布局页面?
- r - 将 any() 与 dbplyr 一起用于 R 中的分组数据库查询
- angular - 角度测试 - 类型错误:无法读取underfined的属性
- python-3.x - 如何在 Python 中将特殊字符转换为 Unicode 转义序列
- javascript - 如何创建一个通用步骤定义文件夹以供 cypress/cucumber 中的所有 .feature 文件使用?
- karate - 空手道:记录 POST 表单参数
- java - 如何修改初始化方法是 Selenium 页面对象模型基类以调用 Selenium Grid Hub 和 Node?
- hibernate - 从 Hibernate 5.3 迁移到 5.4
- python - 将月份从行转为列