Created
July 15, 2014 18:26
-
-
Save sriram-mv/19f692b0962c497e3917 to your computer and use it in GitHub Desktop.
Uasge of asyncio with requests
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 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