Debian Codegraph @

docs/source/Manuals/programs/carwash.py

raw symbols SCIP
1 from SimPy.Simulation import (Process, SimEvent, Store, activate, get,
2 initialize, hold, now, put, simulate, waitevent)
3 """Carwash is master"""
4
5
6 class Carwash(Process):
7 """Carwash is master"""
8 def __init__(self, name):
9 Process.__init__(self, name=name)
10
11 def lifecycle(self):
12 while True:
13 yield get, self, waitingCars, 1
14 carBeingWashed = self.got[0]
15 yield hold, self, washtime
16 carBeingWashed.doneSignal.signal(self.name)
17
18
19 class Car(Process):
20 """Car is slave"""
21 def __init__(self, name):
22 Process.__init__(self, name=name)
23 self.doneSignal = SimEvent()
24
25 def lifecycle(self):
26 yield put, self, waitingCars, [self]
27 yield waitevent, self, self.doneSignal
28 whichWash = self.doneSignal.signalparam
29 print('%s car %s done by %s' % (now(), self.name, whichWash))
30
31
32 class CarGenerator(Process):
33 def generate(self):
34 i = 0
35 while True:
36 yield hold, self, 2
37 c = Car('%d' % i)
38 activate(c, c.lifecycle())
39 i += 1
40
41 washtime = 5
42 initialize()
43
44 # put four cars into the queue of waiting cars
45 for j in range(1, 5):
46 c = Car(name='%d' % -j)
47 activate(c, c.lifecycle())
48
49 waitingCars = Store(capacity=40)
50 for i in range(2):
51 cw = Carwash('Carwash %s' % i)
52 activate(cw, cw.lifecycle())
53
54 cg = CarGenerator()
55 activate(cg, cg.generate())
56 simulate(until=30)
57 print('waitingCars %s' % [x.name for x in waitingCars.theBuffer])