Skip to content

Commit 7ec5f62

Browse files
committed
updated taxi simulator
1 parent c6df60b commit 7ec5f62

File tree

7 files changed

+265
-4
lines changed

7 files changed

+265
-4
lines changed

concurrency/wikipedia/daypicts.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@
2828

2929
SAVE_DIR = 'downloaded/'
3030

31-
#POTD_BASE_URL = 'http://en.wikipedia.org/wiki/Template:POTD/'
32-
POTD_BASE_URL = 'http://127.0.0.1:8001/Template-POTD/'
31+
HTTP_PORT = 8002
32+
POTD_BASE_URL = 'http://en.wikipedia.org/wiki/Template:POTD/'
33+
#POTD_BASE_URL = 'http://127.0.0.1:{}/Template-POTD/'.format(HTTP_PORT)
3334

3435
REMOTE_PICT_BASE_URL = 'http://upload.wikimedia.org/wikipedia/'
35-
LOCAL_PICT_BASE_URL = 'http://127.0.0.1:8001/'
36-
PICT_BASE_URL = LOCAL_PICT_BASE_URL
36+
#LOCAL_PICT_BASE_URL = 'http://127.0.0.1:{}/'.format(HTTP_PORT)
37+
LOCAL_PICT_BASE_URL = REMOTE_PICT_BASE_URL
38+
PICT_BASE_URL = REMOTE_PICT_BASE_URL
3739

3840
POTD_IMAGE_RE = re.compile(r'src="(//upload\..*?)"')
3941
PODT_EARLIEST_TEMPLATE = '2007-01-01'

concurrency/wikipedia/delay.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
vaurien --protocol http --proxy localhost:8002 --backend localhost:8001 \
3+
--behavior 100:delay --behavior-delay-sleep .1

control/adder/adder.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import sys
2+
3+
4+
def ask():
5+
prompt = '>'
6+
while True:
7+
response = input(prompt)
8+
if not response:
9+
return 0
10+
yield response
11+
12+
13+
def parse_args():
14+
yield from iter(sys.argv[1:])
15+
16+
17+
def fetch(producer):
18+
gen = producer()
19+
next(gen)
20+
yield from gen
21+
22+
23+
def main(args):
24+
if args:
25+
producer = parse_args
26+
else:
27+
producer = ask
28+
29+
total = 0
30+
count = 0
31+
gen = fetch(producer())
32+
while True:
33+
term = yield from gen
34+
term = float(term)
35+
total += term
36+
count += 1
37+
average = total / count
38+
print('total: {} average: {}'.format(total, average))
39+
40+
41+
if __name__ == '__main__':
42+
main(sys.argv[1:])

control/adder/coroadder0.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
Closing a generator raises ``GeneratorExit`` at the pending ``yield``
3+
4+
>>> adder = adder_coro()
5+
>>> next(adder)
6+
>>> adder.send(10)
7+
>>> adder.send(20)
8+
>>> adder.send(30)
9+
>>> adder.close()
10+
-> total: 60 terms: 3 average: 20.0
11+
12+
13+
Other exceptions propagate to the caller:
14+
15+
>>> adder = adder_coro()
16+
>>> next(adder)
17+
>>> adder.send(10)
18+
>>> adder.send('spam')
19+
Traceback (most recent call last):
20+
...
21+
TypeError: unsupported operand type(s) for +=: 'int' and 'str'
22+
23+
24+
"""
25+
26+
def adder_coro(initial=0):
27+
total = initial
28+
num_terms = 0
29+
try:
30+
while True:
31+
term = yield
32+
total += term
33+
num_terms += 1
34+
except GeneratorExit:
35+
average = total / num_terms
36+
msg = '-> total: {} terms: {} average: {}'
37+
print(msg.format(total, num_terms, average))

control/adder/yetanother.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import sys
2+
import collections
3+
4+
Result = collections.namedtuple('Result', 'total average')
5+
6+
def adder():
7+
total = 0
8+
count = 0
9+
while True:
10+
term = yield
11+
try:
12+
term = float(term)
13+
except (ValueError, TypeError):
14+
break
15+
else:
16+
total += term
17+
count += 1
18+
return Result(total, total/count)
19+
20+
def process_args(coro, args):
21+
for arg in args:
22+
coro.send(arg)
23+
try:
24+
next(coro)
25+
except StopIteration as exc:
26+
return exc.value
27+
28+
29+
def prompt(coro):
30+
while True:
31+
term = input('+> ')
32+
try:
33+
coro.send(term)
34+
except StopIteration as exc:
35+
return exc.value
36+
37+
38+
def main():
39+
coro = adder()
40+
next(coro) # prime it
41+
if len(sys.argv) > 1:
42+
res = process_args(coro, sys.argv[1:])
43+
else:
44+
res = prompt(coro)
45+
print(res)
46+
47+
main()

control/countdown_yf.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from time import sleep
2+
3+
def countdown(n):
4+
while n:
5+
print('\tn ->', n)
6+
yield n
7+
n -= 1
8+
sleep(1)
9+
10+
def foo():
11+
for i in range(6, 3, -1):
12+
yield i
13+
yield from countdown(3)
14+
15+
#for j in foo():
16+
# print('j ->', j)
17+
18+
19+
def squares(n):
20+
yield from [i for i in range(n)]
21+
yield from [i*i for i in range(n)]
22+
23+
def squares_stupid(n):
24+
for i in range(n):
25+
yield i
26+
27+
for i in range(n):
28+
yield i*i
29+
30+
#for s in squares(10):
31+
# print(s)
32+
33+
34+
def tokenize():
35+
while True:
36+
source = input('> ')
37+
try:
38+
obj = eval(source)
39+
except BaseException:
40+
print('*crash*')
41+
return
42+
try:
43+
it = iter(obj)
44+
except TypeError:
45+
yield obj
46+
return
47+
else:
48+
yield from it
49+
50+
#g = tokenize()
51+
52+
#for res in g:
53+
# print(res)
54+
55+
56+
from concurrent.futures import Future
57+
58+
def f():
59+
f = future()
60+
61+
def foo(fut):
62+
print(fut, fut.result())
63+
f = Future()
64+
f.add_done_callback(foo)
65+
f.set_result(42)
66+
67+
68+
69+

control/simulation/taxi_sim.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import sys
2+
import random
3+
import collections
4+
import queue
5+
6+
Event = collections.namedtuple('Event', 'time actor description')
7+
8+
FIND_CUSTOMER_INTERVAL = 4
9+
TRIP_DURATION = 10
10+
11+
12+
def compute_delay(interval):
13+
return int(random.expovariate(1/interval))
14+
15+
16+
def taxi_process(sim, ident):
17+
trips = 3
18+
for i in range(trips):
19+
prowling_time = compute_delay(FIND_CUSTOMER_INTERVAL)
20+
yield Event(sim.time + prowling_time, ident, 'customer picked up')
21+
22+
trip_time = compute_delay(TRIP_DURATION)
23+
yield Event(sim.time + trip_time, ident, 'customer dropped off')
24+
25+
26+
class Simulator:
27+
28+
def __init__(self):
29+
self.events = queue.PriorityQueue()
30+
self.time = 0
31+
32+
def run(self, end_time):
33+
taxis = [taxi_process(self, i) for i in range(3)]
34+
while self.time < end_time:
35+
for index, taxi in enumerate(taxis):
36+
try:
37+
future_event = next(taxi)
38+
except StopIteration:
39+
del taxis[index] # remove taxi not in service
40+
else:
41+
self.events.put(future_event)
42+
if self.events.empty():
43+
print('*** end of events ***')
44+
break
45+
event = self.events.get()
46+
self.time = event.time
47+
print('taxi:', event.actor, event)
48+
else:
49+
print('*** end of simulation time ***')
50+
51+
52+
def main(args):
53+
if args:
54+
end_time = int(args[0])
55+
else:
56+
end_time = 10
57+
sim = Simulator()
58+
sim.run(end_time)
59+
60+
if __name__ == '__main__':
61+
main(sys.argv[1:])

0 commit comments

Comments
 (0)