简体   繁体   English

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

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

I tried to read https://hackernoon.com/asynchronous-python-45df84b82434 .我试图阅读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.这是关于异步 python 的,我尝试了这个代码,但我得到了一个奇怪的错误。 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:我没有任何名为 ayncio 的文件,我有证据:

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

asyncio.run is a Python 3.7 addition. asyncio.run是 Python 3.7 的补充。 In 3.5-3.6, your example is roughly equivalent to:在 3.5-3.6 中,您的示例大致相当于:

import asyncio

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

The asyncio.run() function was added in Python 3.7. asyncio.run()函数是在 Python 3.7 中添加的。 From theasyncio.run() function documentation :来自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 provisional part ;注意临时部分 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.无论如何,你不能在 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.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.有关完整的asyncio.run()实现,请参阅asyncio.runners源代码

Just in case this is useful to someone else but for me the issue was my file was called asyncio.py .以防万一这对其他人有用,但对我来说问题是我的文件被称为asyncio.py I renamed it to asyncio_example.py and it started to work again (it was failing at the import statement for asyncio).我将它重命名为asyncio_example.py并且它再次开始工作(它在 asyncio 的导入语句中失败)。

This issue helped me realize this: https://github.com/tornadoweb/tornado/issues/2868这个问题帮助我意识到这一点: https : //github.com/tornadoweb/tornado/issues/2868

If anyone is having problem with no current loop try如果有人遇到没有电流循环的问题,请尝试

loop = asyncio.循环=异步。 new _event_loop()新的_event_loop()

result = loop.run_until_complete(coro)结果 = loop.run_until_complete(coro)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 3.6 AttributeError:模块“statsmodels”没有属性“compat” - Python 3.6 AttributeError: module 'statsmodels' has no attribute 'compat' 使用python3.6创建virtualenv时,模块&#39;enum&#39;没有属性&#39;IntFlag&#39;吗? - module 'enum' has no attribute 'IntFlag' when creating virtualenv using python3.6? Python AttributeError:模块“ runAnalytics”没有属性“ run” - Python AttributeError: module 'runAnalytics' has no attribute 'run' Python 3.6 matplot lib 错误:AttributeError:模块“matplotlib”没有属性“projections” - Python 3.6 matplot lib error: AttributeError: module 'matplotlib' has no attribute 'projections' Python 3.6 ~ AttributeError: module &#39;servicemanager&#39; has no attribute &#39;Initialize&#39; ~ 创建windows service exe时 - Python 3.6 ~ AttributeError: module 'servicemanager' has no attribute 'Initialize' ~ When creating windows service exe AttributeError: 模块“asyncio.coroutines”没有属性“debug_wrapper” - AttributeError: module 'asyncio.coroutines' has no attribute 'debug_wrapper' AttributeError:模块“ asyncio”没有属性“ _get_running_loop” - AttributeError: module 'asyncio' has no attribute '_get_running_loop' AttributeError: 模块“asyncio”没有属性“create_task” - AttributeError: module 'asyncio' has no attribute 'create_task' AttributeError: 模块“asyncio.coroutines”没有属性“_is_coroutine” - AttributeError: module 'asyncio.coroutines' has no attribute '_is_coroutine' Python 3.7 错误:AttributeError:模块“子进程”没有属性“运行” - Python 3.7 Error: AttributeError: module 'subprocess' has no attribute 'run'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM