How to stop a process

847 views
Skip to first unread message

arkha

unread,
May 22, 2017, 3:02:12 AM5/22/17
to python-simpy

Hi,

I searched a bit for a simple example showing how to simply stop a process, but couldn't find anything clear.

Suppose I have something simple like:

import simpy

def example(env):
    for i in range(10):
        event = simpy.events.Timeout(env, delay=1, value=42*i)
        value = yield event
        print('now=%d, value=%d' % (env.now, value))

env = simpy.Environment()
example_gen = example(env)
p = simpy.events.Process(env, example_gen)


and then run the simulation unilt say 6:

env.run(until=6)

After this point, I simply want to stop this process, so that if I run

env.run()

essentially I expect nothing else to happen (in particular no error being thrown).

How can I achieve this?

Thanks

James Arruda

unread,
May 22, 2017, 10:15:18 AM5/22/17
to python-simpy
To stop a process you can interrupt it. See the documentation on interrupting:


 You'll have to write some more code to handle the interrupt, otherwise an error will be thrown. 

For example:

def example(env):
   
try:
       
<your code here>
   
except simpy.Interrupt as interrupt:
       
<cleanup code here>

Then call `p.interrupt()` after the first run command. 
Reply all
Reply to author
Forward
Message has been deleted
0 new messages