Considering the code below. I would like to run 3 experiments at a time. The experiments are independent, the only thing they share is the Model object which they only read.
As there are seemingly no hard things in threading this out, how can I best do this in Python? I would like to use a pool or so to make sure that only three experiments run at a time. Shall I use multi-processing? If yes, how is the shortest and most concise?
#!/usr/bin/env python2.6
import time
class Model:
name = ""
def __init__(self,name):
self.name = name
class Experiment:
id = 0
model = None
done = False
def __init__(self,id,model):
self.id = id
self.model = model
def run(self):
for _ in range(0,60):
print "Hey %s from experiment %d" % (self.model.name, id)
time.sleep(1)
self.done = True
if __name__ == "__main__":
experiments = []
model = Model("statictistical model")
for i in range(0,5):
experiments.append(Experiment(i, model))
#How to run 3 experiments at the same time