-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path08_simpy5.py
More file actions
44 lines (33 loc) · 1.25 KB
/
08_simpy5.py
File metadata and controls
44 lines (33 loc) · 1.25 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
36
37
38
39
40
41
42
43
44
# 08_simpy5.py
import simpy
import random
def waiter(env, kitchen):
# the time at which the preparation is interrupted
yield env.timeout(5)
# We use the interrupt () method of cooking process
kitchen.action.interrupt()
class Kitchen:
def __init__(self, env):
self.env = env
self.action = env.process(self.run())
def run(self):
while True:
print('A dish begins to be prepared t={0}'.format(self.env.now))
try:
yield self.env.process(self.preparation())
except simpy.Interrupt:
# If the preparation of the dish takes more time than the
# permitted delay time should be discontinued and go to the
# next stage
print('[WAITER] the preparation takes a',
'long time, place in the oven as it is')
print('A dish is put in the oven t={0}'.format(self.env.now))
cooking_time = random.randint(5, 10)
yield self.env.timeout(cooking_time)
def preparation(self):
yield self.env.timeout(random.randint(5, 20))
maximum_time = 80
env = simpy.Environment()
k = Kitchen(env)
env.process(waiter(env, k)) # waiter activated
env.run(until=maximum_time)