Skip to content

Instantly share code, notes, and snippets.

@nhumrich
Last active September 27, 2018 19:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhumrich/933445f9de156b02950f3dacdd3ba9bb to your computer and use it in GitHub Desktop.
Save nhumrich/933445f9de156b02950f3dacdd3ba9bb to your computer and use it in GitHub Desktop.
async blog example for async/await
import asyncio
import aiohttp
urls = ['http://www.google.com', 'http://www.yandex.ru', 'http://www.python.org']
async def call_url(url):
print('Starting {}'.format(url))
response = await aiohttp.ClientSession().get(url)
data = await response.text()
print('{}: {} bytes: {}'.format(url, len(data), data))
return data
futures = [call_url(url) for url in urls]
asyncio.run(asyncio.wait(futures))
@ankitml
Copy link

ankitml commented Jun 14, 2018

aiohttp.get doesnt exist (python version maybe). Using aiohttp.request('GET', url) instead.

But further, I am getting

  File "crawler.py", line 8, in call_url
    response = await aiohttp.request('GET', url)
TypeError: object _SessionRequestContextManager can't be used in 'await' expression

while running this on python 3.6. Any ideas ?

@nhumrich
Copy link
Author

Sorry about the late comment. Aiohttp has since removed this method. I will need to update the example.

@nhumrich
Copy link
Author

Updated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment