Skip to content

Instantly share code, notes, and snippets.

@ustun
Created November 3, 2016 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ustun/38906bdda4e9ff0ab29007a564341aae to your computer and use it in GitHub Desktop.
Save ustun/38906bdda4e9ff0ab29007a564341aae to your computer and use it in GitHub Desktop.
import asyncio
import requests
def get_that_does_not_fail(url):
try:
response = requests.get(url)
return {"success": True, "response": response}
except Exception as e:
return {"success": False, "error": e, "url": url}
async def async_get(url):
print("starting request")
response = await loop.run_in_executor(None, get_that_does_not_fail, url)
print("got response")
return response
async def foo(i):
print("started", i)
await asyncio.sleep(1)
print("finished", i)
async def main():
# await foo(3)
# await foo(4)
await asyncio.wait([foo(3), foo(4)])
urls = [
"http://hurriyet.com.tr",
"http://sabah.com.tr",
"http://milliyet.com.tr",
"http://ustunyyy.com",
]
results = await asyncio.gather(*[async_get(url) for url in urls])
for result in results:
if result['success']:
print(result['response'].ok, result['response'].url)
else:
print("Failure", result['url'], result['error'])
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment