You can use Python's multiprocessing package to implement process-based parallelism.
Here is some example code for Gurobi 9 using a Pool:
import multiprocessing as mp
import gurobipy as gp
with gp.Env() as env, gp.Model(env=env) as model:
# define model
# retrieve data from model
if __name__ == '__main__':
with mp.Pool() as pool:
pool.map(solve_model, [input_data1, input_data2, input_data3]
Note though that thread-based parallelism -- e.g. using the threading module -- is not possible since currently the gurobipy module is not thread-safe.
Each process should create its own environment when using multiprocessing. Also, note that you should make sure to properly close your environments and dispose of your models. Starting with Gurobi 9, you can use the following pattern, that will automatically discard your model and environment upon leaving the with-block:
with gp.Env() as env, gp.Model(env=env) as model: # remaining model code
With Gurobi 8 and earlier, you have to use:
env = gp.Env()
model = gp.Model(env=env)
# remaining model code
Issues on macOS 10.13 and later
The multiprocessing package supports different methods for starting the subprocesses. Until Python 3.7 the default method on macOS was forking. However, on macOS 10.13 and later there are some issues with that: Fork without exec may crash the subprocess; see here. In this case, "spawn" should be used instead. To change that, you can either use
to change the setting globally. Or use
with mp.get_context("spawn").Pool() as pool:
to change it only for one Pool.
Starting with Python 3.8, spawning is already the default on macOS.
See here for more information.