Skip to content

Instantly share code, notes, and snippets.

@samuelcolvin
Last active May 1, 2016 16:43
Show Gist options
  • Save samuelcolvin/14261a8bf0299e16a6507c33ce49f3df to your computer and use it in GitHub Desktop.
Save samuelcolvin/14261a8bf0299e16a6507c33ce49f3df to your computer and use it in GitHub Desktop.
Asyncio concurrency
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()
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