Skip to content

Instantly share code, notes, and snippets.

@sriram-mv
Created July 15, 2014 18:26
Show Gist options
  • Select an option

  • Save sriram-mv/19f692b0962c497e3917 to your computer and use it in GitHub Desktop.

Select an option

Save sriram-mv/19f692b0962c497e3917 to your computer and use it in GitHub Desktop.
Uasge of asyncio with requests
import asyncio
import requests
def fetch_page(url, idx):
#loop = asyncio.get_event_loop()
url = 'https://bing.com'
future = loop.run_in_executor(None, requests.get, 'http://www.bing.com')
response = yield from future
if response.status_code == 200:
print("data fetched successfully for: %d" % idx)
else:
print("data fetch failed for: %d" % idx)
print(response.content, response.status)
def main():
url = 'https://bing.com'
urls = [url]*100
coros = []
for idx, url in enumerate(urls):
coros.append(asyncio.Task(fetch_page(url, idx)))
yield from asyncio.gather(*coros)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment