Skip to content

Instantly share code, notes, and snippets.

@brodul
Last active January 4, 2020 11:55
Show Gist options
  • Save brodul/4cffd3cf7fd4b026fdb9bc4c5aa2f11e to your computer and use it in GitHub Desktop.
Save brodul/4cffd3cf7fd4b026fdb9bc4c5aa2f11e to your computer and use it in GitHub Desktop.
concurrency in python in 2020
import urllib.request
import asyncio
loop = asyncio.get_event_loop()
loop.set_debug(True)
def get(url):
with urllib.request.urlopen(url) as f:
return f.read()
async def wrapped_get(url):
return await loop.run_in_executor(None, get, url)
async def main():
results = await asyncio.gather(*[ wrapped_get(u) for u in [
'https://n161.tech/api/dummyapi/user/1',
'https://n161.tech/api/dummyapi/user/2',
]])
for result in results:
result = result.decode('utf-8')
print(result)
loop.run_until_complete(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment