1

I recently downloaded and installed Anaconda/Spyder with Python 3.6-64 bit on a Win10 computer and installed simpy to try to learn more about discrete event simulations. I am new to both simpy, python, and object oriented program so my question may sound pretty stupid.

One of the first examples in the documentation is the car example interrupted by another process. I copied this example (shown below) and ran it successfully. But if I changed timeout in the driver function from 3 to 6 the script crashes. The way I thought that the simulation would proceed with an interrupt time of 6 would be that the interrupt would occur on the second charging event that begins at time=5. Instead the script crashes with the following output. Can someone explain to me why this doesn't work?

The error

runfile('C:/Users/admin/Documents/Python Scripts/carSimInterruptRevA.py', wdir='C:/Users/admin/Documents/Python Scripts') Start parking and charging at 0 Start driving at 5 Traceback (most recent call last):

File "", line 1, in runfile('C:/Users/admin/Documents/Python Scripts/carSimInterruptRevA.py', wdir='C:/Users/admin/Documents/Python Scripts')

File "C:\Users\admin\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile execfile(filename, namespace)

File "C:\Users\admin\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/admin/Documents/Python Scripts/carSimInterruptRevA.py", line 38, in env.run(until=30)

File "C:\Users\admin\Anaconda3\lib\site-packages\simpy\core.py", line 138, in run self.step()

File "C:\Users\admin\Anaconda3\lib\site-packages\simpy\core.py", line 230, in step raise exc

Interrupt: Interrupt(None)I

The example code

import simpy
def driver(env, car):
    yield env.timeout(3)
    car.action.interrupt()

class Car(object):
    def __init__(self, env):
        self.env = env
        self.action = env.process(self.run())

    def run(self):
        while True:
            print('Start parking and charging at %d' % self.env.now)
            charge_duration = 5
            # We may get interrupted while charging the battery
            try:
                yield self.env.process(self.charge(charge_duration))
            except simpy.Interrupt:
                # When we received an interrupt, we stop charging and
                # switch to the "driving" state
                print('Was interrupted. Hope, the battery is full enough ...')

            print('Start driving at %d' % self.env.now)
            trip_duration = 2
            yield self.env.timeout(trip_duration)
    def charge(self, duration):
        yield self.env.timeout(duration)

env = simpy.Environment()
car = Car(env)
env.process(driver(env, car))
env.run(until=30)
cel
  • 30,017
  • 18
  • 97
  • 117
user19633
  • 11
  • 1
  • Likely it is failing when the interrupt happens while the car isn’t charging. If you need to handle the interrupt in any state of Car, you need to add the try/except on every car state (yield statement), in this case add to the driving state too. – DisappointedByUnaccountableMod Oct 28 '18 at 09:42
  • You are right barny. Thanks. I thought the interrupt was occurring while the car was in its second charge. When I used the right interrupt time everything worked as expected. I guess I need to learn to add. – user19633 Oct 28 '18 at 17:05

0 Answers0