Python3.6 AttributeError:模块“asyncio”没有“run”属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52796630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:11:42  来源:igfitidea点击:

Python3.6 AttributeError: module 'asyncio' has no attribute 'run'

pythonpython-3.6python-asyncio

提问by Jirka Svítil

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: `

我试图阅读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))

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:

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

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

回答by Norrius

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

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

import asyncio

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

回答by Martijn Pieters

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

asyncio.run()函数是在 Python 3.7 中添加的。从asyncio.run()功能文档

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

3.7 版新功能:重要提示:此函数已临时添加到 Python 3.7 中的 asyncio。

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

注意临时部分;Python 维护者预见到该函数可能需要进一步调整和更新,因此 API 可能会在未来的 Python 版本中发生变化。

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

无论如何,你不能在 Python 3.6 上使用它。您必须升级或实施自己的。

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

一个非常简单的近似是使用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.runnerssource codefor the complete asyncio.run()implementation.

尽管这忽略了处理可能仍在运行的剩余任务。查看完整实现的asyncio.runners源代码asyncio.run()