95

I tried to read https://hackernoon.com/asynchronous-python-45df84b82434. It's about asynchronous python and I tried the code from this, but I'm getting a weird Error. The code is: `

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))

When I try to run it says:

Traceback (most recent call last):
  File "test.py", line 15, in <module>
    asyncio.run(asyncio.wait(futures))
AttributeError: module 'asyncio' has no attribute 'run'
sys:1: RuntimeWarning: coroutine 'call_url' was never awaited

I dont have any files named ayncio and I have proof:

>>> asyncio
<module 'asyncio' from '/usr/lib/python3.6/asyncio/__init__.py'>
user4815162342
  • 141,790
  • 18
  • 296
  • 355
Jirka Svítil
  • 1,134
  • 1
  • 7
  • 10

4 Answers4

121

asyncio.run is a Python 3.7 addition. In 3.5-3.6, your example is roughly equivalent to:

import asyncio

futures = [...]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(futures))
Norrius
  • 7,558
  • 5
  • 40
  • 49
  • 10
    When you grab the existing event loop, I'd not close it.. `asyncio.run()` only closes the loop because it actually creates a new loop to start with. – Martijn Pieters Oct 13 '18 at 19:53
  • You might want to use `asyncio.gather` instead of `asyncio.wait` to actually consume the futures and handle exceptions. – mkaptur May 10 '21 at 08:10
56

The asyncio.run() function was added in Python 3.7. From the asyncio.run() function documentation:

New in version 3.7: Important: this function has been added to asyncio in Python 3.7 on a provisional basis.

Note the provisional part; the Python maintainers forsee that the function may need further tweaking and updating, so the API may change in future Python versions.

At any rate, you can't use it on Python 3.6. You'll have to upgrade or implement your own.

A very simple approximation would be to use loop.run_until_complete():

loop = asyncio.get_event_loop()
result = loop.run_until_complete(coro)

although this ignores handling remaining tasks that may still be running. See the asyncio.runners source code for the complete asyncio.run() implementation.

user4815162342
  • 141,790
  • 18
  • 296
  • 355
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
10

Just in case this is useful to someone else but for me the issue was my file was called asyncio.py. I renamed it to asyncio_example.py and it started to work again (it was failing at the import statement for asyncio).

This issue helped me realize this: https://github.com/tornadoweb/tornado/issues/2868

Charlie Parker
  • 5,884
  • 57
  • 198
  • 323
0

If anyone is having a problem with no current loop try:

loop = asyncio.**new**_event_loop()

result = loop.run_until_complete(coro)
RiveN
  • 2,595
  • 11
  • 13
  • 26
Rappy Mic
  • 1
  • 1