File: carwash.py

package info (click to toggle)
python-simpy 2.3.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,864 kB
  • sloc: python: 11,171; makefile: 143
file content (57 lines) | stat: -rw-r--r-- 1,530 bytes parent folder | download | duplicates (4)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
from SimPy.Simulation import (Process, SimEvent, Store, activate, get,
        initialize, hold, now, put, simulate, waitevent)
"""Carwash is master"""


class Carwash(Process):
    """Carwash is master"""
    def __init__(self, name):
        Process.__init__(self, name=name)

    def lifecycle(self):
        while True:
            yield get, self, waitingCars, 1
            carBeingWashed = self.got[0]
            yield hold, self, washtime
            carBeingWashed.doneSignal.signal(self.name)


class Car(Process):
    """Car is slave"""
    def __init__(self, name):
        Process.__init__(self, name=name)
        self.doneSignal = SimEvent()

    def lifecycle(self):
        yield put, self, waitingCars, [self]
        yield waitevent, self, self.doneSignal
        whichWash = self.doneSignal.signalparam
        print('%s car %s done by %s' % (now(), self.name, whichWash))


class CarGenerator(Process):
    def generate(self):
        i = 0
        while True:
            yield hold, self, 2
            c = Car('%d' % i)
            activate(c, c.lifecycle())
            i += 1

washtime = 5
initialize()

# put four cars into the queue of waiting cars
for j in range(1, 5):
        c = Car(name='%d' % -j)
        activate(c, c.lifecycle())

waitingCars = Store(capacity=40)
for i in range(2):
        cw = Carwash('Carwash %s' % i)
        activate(cw, cw.lifecycle())

cg = CarGenerator()
activate(cg, cg.generate())
simulate(until=30)
print('waitingCars %s' % [x.name for x in waitingCars.theBuffer])