简体   繁体   English

Gurobi的多处理兼容性问题

[英]multiprocessing compatibility issue with Gurobi

The following simple multiprocessing of a square function works fine: 以下对平方函数的简单多处理工作正常:

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

But when I add an initialization of a Gurobi model in the class like this, 但是当我在这样的类中添加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()

It returns the following error: 它返回以下错误:

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

A serial version with Gurobi works fine: 具有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()

The Gurobi API does not support sharing a single Gurobi environment across multiple processes or threads. Gurobi API不支持在多个进程或线程之间共享单个Gurobi环境。 If you want to solve multiple modules in multiple processes or threads, you must explicitly create multiple Gurobi environments in each process or thread. 如果要解决多个进程或线程中的多个模块,则必须在每个进程或线程中显式创建多个Gurobi环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM