SimPy docs -- Interrupt process question

404 views
Skip to first unread message

experience

unread,
Jul 5, 2022, 12:43:24 PM7/5/22
to python-simpy
I'm going through the topical guides of SimPy, in particular the one on Process Interactions. 

There is something said in passing at the end of the section: 

Since we don’t do anything special to the original target event of the process, the interrupted process can yield the same event again after catching the Interrupt – Imagine someone waiting for a shop to open. The person may get interrupted by a phone call. After finishing the call, he or she checks if the shop already opened and either enters or continues to wait.

I find that this is unclear and would benefit from an example. From the last example code given, it looks like whenever a process is interrupted, it won't ever go back to waiting for whatever if was originally waiting for. How does that work out?

Thanks in advance.

Michael Gibbs

unread,
Jul 6, 2022, 9:06:03 PM7/6/22
to python-simpy
The real point the doc is making is just because your process gets interrupted, your process dose not need to end.  The interrupt is a exception and just like any other exception handlers, all the variable (and events) of the process are still in scope.  So one of your options for handling the interrupt is to resume yielding to the same event you were yielded to when you got interrupted.  This is one way processes can communicate, though event callbacks may be a better option.  Here is a quick demo where I resume on the first interrupt, but stop on the second

"""
    Simple demo on how to handel process interrupts

    Programmer: Michael R. Gibbs
"""

from re import I
import simpy

def the_process(env):
    """
        simple process that will get interrupted
    """

    i_cnt = 0   # count the number of interuptions
    not_done = True # flags when done

    # event that process yields to
    the_event = env.timeout(15)

    print(f'{env.now:0.2f} the process has started')

    # loop until done
    while not_done:
        try:
            yield the_event

        except simpy.Interrupt as i:
            # have been interupted
            i_cnt += 1
            print(f'{env.now:0.2f} the process has been intrruped {i_cnt} times {i.cause}')

            # there are several ways you can process the interupt
            if i_cnt == 1:
                # process the error and resume
                # the loop will yield to the same event as before, not a new event
                # because the interupt does not end other events, even events
                # created by this process
                print(f'{env.now:0.2f} the process handeled the intrupt and resumed')

            elif i_cnt == 2:
                # process the error and exit the process
                # by stopping the loop
                not_done = False
                print(f'{env.now:0.2f} the process handeled the intrupt and is stopping')

            # third option is to rethrow the interupt and let a parrent process deal with it

    print(f'{env.now:0.2f} the process has ended')


def the_interrupter(env, the_process):
    """
        interupts a process two time
    """
       
    yield env.timeout(5)
    print(f'{env.now:0.2f} sending first interrupt')
    the_process.interrupt("first interrupt")

    yield env.timeout(5)
    print(f'{env.now:0.2f} sending second interrupt')
    the_process.interrupt("second interrupt")

env = simpy.Environment()
process1 = env.process(the_process(env))
env.process(the_interrupter(env, process1))

env.run(100)



experience

unread,
Jul 7, 2022, 4:15:16 PM7/7/22
to python-simpy
Thanks a lot for this explanation and the example, that makes a lot of sense!
Reply all
Reply to author
Forward
0 new messages