Python3.6 属性错误: module 'asyncio' has no attribute 'run'

标签 python python-3.6 python-asyncio

我尝试阅读 https://hackernoon.com/asynchronous-python-45df84b82434 . 它是关于异步 python 的,我尝试了其中的代码,但我遇到了一个奇怪的错误。 代码是: `

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

当我尝试运行时它说:

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

我没有任何名为 ayncio 的文件,但我有证据:

>>> asyncio
<module 'asyncio' from '/usr/lib/python3.6/asyncio/__init__.py'>

最佳答案

asyncio.run是 Python 3.7 的补充。在3.5-3.6中,你的例子大致相当于:

import asyncio

futures = [...]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(futures))

关于Python3.6 属性错误: module 'asyncio' has no attribute 'run' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52796630/

相关文章:

python - 将生成器管道重构为协程的最佳方法是什么?

python - 当我获取图像名称并用 'IndexError: list index out of range' 分割时遇到此 ',' 错误。

python - 检查位掩码的特定位

python - 截断不正确的日期值之谜

python - 将 2 个或更多异步 HTTP 调用的结果设置为命名变量

带有异步 def 的 python asyncio add_done_callback

python - multiprocessing.Process 实例完成后我的队列为空

python - 模块未找到错误 : No module named 'BaseHTTPServer'

python - 如何在Python 3.5中恢复文件下载?

python - 从所有数据框列中删除子字符串