Skip to content

Instantly share code, notes, and snippets.

@pathcl
Created October 13, 2015 19:00
Show Gist options
  • Save pathcl/2d58759fd2559f77cc3d to your computer and use it in GitHub Desktop.
Save pathcl/2d58759fd2559f77cc3d to your computer and use it in GitHub Desktop.
ex2.py
import asyncio
import aiohttp
def fetch_page(url, idx):
url = 'https://yahoo.com'
response = yield from aiohttp.request('GET', url)
if response.status == 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://yahoo.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