import simpy
class Worker:
def __init__(self, env):
self.env = env
# self.break_proc = env.process(self.break_time())
def work(self):
while True:
# two different tasks, w/ different durations
# 'Task 20' finishes before 'Task 40' starts
# if you put try/except here, 'Task 20' will
# resume after interrupt, even though may be
# on 'Task 40'
yield self.env.process(self.task(20))
yield self.env.process(self.task(40))
def task(self, duration):
while duration:
# I put try/except at 'task level'
# following Machine Shop example
try:
start = self.env.now
yield self.env.timeout(duration)
print(f'Task {duration} finished at {env.now}')
duration = 0 # Exits while loop
except simpy.Interrupt:
duration -= self.env.now - start
# def break_time(self):
# yield self.env.timeout(10) # just picked 10, can be anything
# I would like to interrupt the work process
# and somehow this gets propagated to the 'in progress' task
# self.work_proc.interrupt()
env = simpy.Environment()
w = Worker(env)
env.run(until=100)
Task 20 finished at 20
Task 40 finished at 60
Task 20 finished at 80