-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimpy_tutorial.py
More file actions
35 lines (30 loc) · 1.06 KB
/
simpy_tutorial.py
File metadata and controls
35 lines (30 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# -*- coding: utf-8 -*-
import simpy
class Car(object):
def __init__(self, env):
self.env = env
# Start the run process everytime an instance is created.
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 yield the process that process() returns
# to wait for it to finish
try:
yield self.env.process(self.charge(charge_duration))
except simpy.Interrupt:
print('Was interrupted, hope battery is full...')
# The charge process has finished and
# we can start driving again.
trip_duration = 2
yield self.env.timeout(trip_duration)
def charge(self, duration):
yield self.env.timeout(duration)
def driver(env, car):
yield env.timeout(3)
car.action.interrupt()
env = simpy.Environment()
car = Car(env)
env.process(driver(env,car))
env.run(until=20)