I am a newbie to SimPy and am trying to get the following done. Simulate processes, where each has a different time delay. Once the first process is done, i am interrupting other processes, which i catch. But once in the catch block all these processes resume again and the environment clock continues till all of them are done. I want the simulation to stop right after the first processes is done and then update remaining time on other processes and basically stop everything so the env.now time is at when the first process is done.
Sample code
class Main:
env = simpy.Environment()
res1 = Resource(env, list(), 10)
res2 = Resource(env, list(), 15)
res3 = Resource(env, list(), 20)
#Add other resources to each
res1.appendOtherResource(res2)
res1.appendOtherResource(res3)
res2.appendOtherResource(res1)
res2.appendOtherResource(res3)
res3.appendOtherResource(res2)
res3.appendOtherResource(res1)
Resources = list()
Resources.append(res1)
Resources.append(res2)
Resources.append(res3)
env.process(scheduleResources(Resources, env))
env.run()
def scheduleResources(Resources, env)
for resource in Resources:
env.process(resource.start())
Class Resource has a start and run method with the following: Also assume each resource has a processing time when it is instantiated: Each resource also has a reference to other resources, not showing code for all that to keep it short.
class Resource:
def __init__(self, env, otherResources, timeToProcess):
self.resource_process = None
self.otherResources = otherResources
self.env = env
self.timeToProcess = timeToProcess
def appendOtherResource(self, otherResource):
self.otherResources.append(otherResource)
def start(self):
yield (self.env.timeout(0))
self.resource_process = self.env.process(self.run())
def run(self):
try:
self.env.timeout(self.timeToProcess)
self.timeToProcess = 0
for res in self.otherResources:
if res.resource_process != None:
if res.resource_process.is_alive:
res.resource_process.interrupt()
except simpy.Interrupt as interrupt:
self.timeToProcess = self.timeToProcess - self.env.now
#How do i ensure that the process that is interrupted is killed after this step and does not continue
What happens currently is that the shortest resource process is done. Interrupt is called on all other resources, and the timeToProcess is updated correctly. But after this the environment runs to complete all resource processes and the env.now changes beyond the first stop that was intended.
I tried env.exit() but with no success. Is there any way in simpy to stop simulation immediately, no matter what processes are scheduled.
Regards