python - Gurobi 的多处理兼容性问题

标签 python python-multiprocessing gurobi

以下简单的平方函数多重处理效果很好:

from multiprocessing import Pool

class A(object):

    def square(self, x):
        return x * x

    def test(self):
        pool = Pool()
        result = pool.map(self.square, range(5))
        return result

但是当我像这样在类中添加 Gurobi 模型的初始化时,

from multiprocessing import Pool
from gurobipy import *

class A(object):

    def __init__(self):
        self.b = Model()

    def square(self, x):
        return x * x

    def test(self):
        pool = Pool()
        result = pool.map(self.square, range(5))
        return result

A().test()

它返回以下错误:

File "model.pxi", line 290, in gurobipy.Model.__getattr__ (../../src/python/gurobipy.c:53411)
KeyError: '__getstate__'

Gurobi 的串行版本工作正常:

from gurobipy import *

class A(object):

    def __init__(self):
        self.b = Model()

    def square(self, x):
        return x * x

    def test(self):
        res = [self.square(i) for i in range(5)]
        return res

A().test()

最佳答案

Gurobi API 不支持跨多个进程或线程共享单个 Gurobi 环境。如果要在多个进程或线程中解决多个模块,则必须在每个进程或线程中显式创建多个Gurobi环境。

关于python - Gurobi 的多处理兼容性问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50571191/

相关文章:

python - 使用弯曲(路径跟踪)向量在 python 中进行流可视化

python - 对于 Python http.server.HTTPServer,是否有 RewriteRule/.htaccess 的替代方案?

python - 不理解 model.getAttr() 的参数

python - 通过 Python 函数跟踪*最大*内存使用情况

python - 当我捕捉到异常时,如何获取上一帧的类型、文件和行号?

python - 如何通过多处理在单独的 Python 进程中使用用 Cython 包装的外部 C 库?

python - 如何防止并行 python 以 "OSError: [Errno 35] Resource temporarily unavailable"退出?

Python 多处理输出

python - 获取Gurobi优化的中间结果

java - Gurobi 无法添加约束