与Gurobi的多处理兼容性问题

时间:2018-05-28 17:33:57

标签: python python-multiprocessing gurobi python-pool

方形函数的以下简单多处理工作正常:

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()

1 个答案:

答案 0 :(得分:1)

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