Last active
January 4, 2020 11:55
-
-
Save brodul/4cffd3cf7fd4b026fdb9bc4c5aa2f11e to your computer and use it in GitHub Desktop.
concurrency in python in 2020
This file contains hidden or 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 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