Last active
May 1, 2016 16:43
-
-
Save samuelcolvin/14261a8bf0299e16a6507c33ce49f3df to your computer and use it in GitHub Desktop.
Asyncio concurrency
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
q = asyncio.Queue() | |
async def produce(): | |
for i in range(10): | |
await q.put(i) | |
async def consume(): | |
while True: | |
try: | |
value = q.get_nowait() | |
except asyncio.QueueEmpty: | |
return | |
await asyncio.sleep(0.5) | |
print("Consumed", value) | |
loop = asyncio.get_event_loop() | |
loop.create_task(produce()) | |
f = asyncio.gather(consume(), consume(), consume()) | |
loop.run_until_complete(f) | |
loop.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import aiohttp | |
async def download(url): | |
s = loop.time() | |
with aiohttp.ClientSession(loop=loop) as session: | |
async with session.get(url) as r: | |
print(url, 'connected') | |
text = await r.text() | |
t = loop.time() - s | |
r = '{:20s} response: {:<4d} size: {:<7d} time: {:0.3f}s'.format(url, r.status, len(text), t) | |
print(r) | |
return t | |
# approximately slowest to fastest order | |
urls = [ | |
'https://yahoo.com', | |
'https://microsoft.com', | |
'https://github.com', | |
'https://facebook.com', | |
'https://google.com', | |
'https://bbc.co.uk', | |
] | |
loop = asyncio.get_event_loop() | |
cos = [download(u) for u in urls] | |
f = asyncio.gather(*cos) | |
loop.run_until_complete(f) | |
total_time = sum(f.result()) | |
print('total time {:0.3f}s'.format(total_time)) | |
loop.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment