Skip to content

Commit c1e50e4

Browse files
committed
demos enhanced during workshop at Garoa
1 parent fe0db0f commit c1e50e4

File tree

6 files changed

+68
-14
lines changed

6 files changed

+68
-14
lines changed

concurrency/Concurrency.mm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<map version="0.9.0">
2+
<!-- To view this file, download free mind mapping software FreeMind from http://freemind.sourceforge.net -->
3+
<node CREATED="1421704544481" ID="ID_1903556943" MODIFIED="1421714803674" TEXT="Concurrency">
4+
<node CREATED="1421704705118" ID="ID_598225085" MODIFIED="1421714763865" POSITION="left" TEXT="threading"/>
5+
<node CREATED="1421704717848" ID="ID_1699689274" MODIFIED="1421704738402" POSITION="left" TEXT="multiprocessing"/>
6+
<node CREATED="1421704568997" ID="ID_48551069" MODIFIED="1421714690188" POSITION="left" TEXT="parallelism"/>
7+
<node CREATED="1421705811900" ID="ID_740158806" MODIFIED="1421714796092" POSITION="right" TEXT="event-based">
8+
<node CREATED="1421706014013" ID="ID_1223527993" MODIFIED="1421706021925" TEXT="callbacks"/>
9+
<node CREATED="1421706028536" ID="ID_151937226" MODIFIED="1421706038694" TEXT="coroutines"/>
10+
</node>
11+
</node>
12+
</map>

concurrency/http_charserver.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python3
22

3+
import sys
34
import asyncio
45
from aiohttp import web
56

@@ -55,26 +56,28 @@ def handle(request):
5556
for word in sorted(EXAMPLE_WORDS, key=str.upper))
5657
text = PAGE_TPL.format(query=query, result=res,
5758
message=msg, links=links)
59+
print('Sending {} results'.format(len(lines)))
5860
return web.Response(content_type=CONTENT_TYPE, text=text)
5961

6062

6163
@asyncio.coroutine
62-
def init(loop):
64+
def init(loop, address, port):
6365
app = web.Application(loop=loop)
6466
app.router.add_route('GET', '/', handle)
6567

6668
server = yield from loop.create_server(app.make_handler(),
67-
'127.0.0.1', 8080)
69+
address, port)
6870
host = server.sockets[0].getsockname()
6971
print('Serving on {}. Hit CTRL-C to stop.'.format(host))
7072

7173

72-
def main():
74+
def main(address="127.0.0.1", port=8888):
75+
port = int(port)
7376
loop = asyncio.get_event_loop()
74-
loop.run_until_complete(init(loop))
77+
loop.run_until_complete(init(loop, address, port))
7578
loop.run_forever()
7679

77-
80+
7881
if __name__ == '__main__':
7982
index = UnicodeNameIndex()
80-
main()
83+
main(*sys.argv[1:])

concurrency/tcp_charserver.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python3
22

3+
import sys
34
import asyncio
45

56
from charfinder import UnicodeNameIndex
@@ -13,8 +14,8 @@
1314
@asyncio.coroutine
1415
def handle_queries(reader, writer):
1516
while True:
16-
writer.write(PROMPT)
17-
yield from writer.drain()
17+
writer.write(PROMPT) # can't yield from!
18+
yield from writer.drain() # must yield from!
1819
data = yield from reader.readline()
1920
try:
2021
query = data.decode().strip()
@@ -29,15 +30,18 @@ def handle_queries(reader, writer):
2930
if lines:
3031
writer.writelines(line.encode() + CRLF for line in lines)
3132
writer.write(index.status(query, len(lines)).encode() + CRLF)
33+
3234
yield from writer.drain()
35+
print('Sent {} results'.format(len(lines)))
3336

3437
print('Close the client socket')
3538
writer.close()
3639

3740

38-
def main():
41+
def main(address='127.0.0.1', port=8888):
42+
port = int(port)
3943
loop = asyncio.get_event_loop()
40-
coro = asyncio.start_server(handle_queries, '127.0.0.1', 8888, loop=loop)
44+
coro = asyncio.start_server(handle_queries, address, port, loop=loop)
4145
server = loop.run_until_complete(coro)
4246

4347
host = server.sockets[0].getsockname()
@@ -54,4 +58,4 @@ def main():
5458

5559
if __name__ == '__main__':
5660
index = UnicodeNameIndex()
57-
main()
61+
main(*sys.argv[1:])

concurrency/timer.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33
@asyncio.coroutine
44
def show_remaining():
5-
remaining = 5
6-
while remaining:
5+
for remaining in range(5, 0, -1):
76
print('Remaining: ', remaining)
87
yield from asyncio.sleep(1)
9-
remaining -= 1
108

119
def main():
1210
loop = asyncio.get_event_loop()

concurrency/timer_clo.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import sys
2+
import asyncio
3+
4+
def make_show_remaining(seconds):
5+
remaining = seconds
6+
7+
def show_remaining(loop):
8+
nonlocal remaining
9+
print('Remaining: ', remaining)
10+
remaining -= 1
11+
if remaining:
12+
loop.call_later(1, show_remaining, loop)
13+
else:
14+
loop.stop()
15+
16+
return show_remaining
17+
18+
19+
def main(seconds=5):
20+
seconds = int(seconds)
21+
loop = asyncio.get_event_loop()
22+
try:
23+
loop.call_soon(make_show_remaining(seconds), loop)
24+
loop.run_forever()
25+
finally:
26+
loop.close()
27+
28+
if __name__ == '__main__':
29+
main(*sys.argv[1:])

concurrency/timer_seq.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import time
2+
3+
def main():
4+
for remaining in range(5, 0, -1):
5+
print('Remaining: ', remaining)
6+
7+
if __name__ == '__main__':
8+
main()

0 commit comments

Comments
 (0)