-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe.py
56 lines (38 loc) · 1.18 KB
/
pipe.py
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
__author__ = 'nick'
import simpy
SIM_DURATION = 100
class Pipe(object):
"""This class represents the propagation through a cable."""
def __init__(self, env, delay):
self.env = env
self.delay = delay
self.store = simpy.Store(env)
def latency(self, value):
yield self.env.timeout(self.delay)
self.store.put(value)
def put(self, value):
self.env.process(self.latency(value))
def get(self):
return self.store.get()
def sender(env, cable):
"""A process which randomly generates messages."""
while True:
# wait for next transmission
yield env.timeout(5)
cable.put('Sender sent this at %d' % env.now)
def receiver(env, cable):
"""A process which consumes messages."""
while True:
# Get event for message pipe
msg = yield cable.get()
print('Received this at %d while %s' % (env.now, msg))
def main():
# Setup and start the simulation
print('Event Latency')
env = simpy.Environment()
cable = Pipe(env, 10)
env.process(sender(env, cable))
env.process(receiver(env, cable))
env.run(until=SIM_DURATION)
if __name__=="__main__":
main()