当前位置: 首页 > 面试题库 >

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

袁何平
2023-03-14
问题内容

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


 类似资料:
  • 问题内容: 我有一个包含许多文件的目录。每个文件定义一些类。我的目录中也有一个空白。 例如: 我正在尝试导入和访问所有这些文件中定义的类: 这给我一个错误的说法,即没有属性。为什么?为什么我只能访问其中一个文件(),而不能访问其他文件? 问题答案: 问题是子模块不会自动导入。您必须显式导入模块: 如果您真的坚持要在导入时可用,则可以输入以下内容: 然后,这将按预期工作:

  • 问题内容: 我正在尝试建立一个ARIMA进行异常检测。我需要找到时间序列图的移动平均值,为此我尝试使用pandas 0.23 错误:跟踪(最近一次调用最近):文件“ C:\ Program Files \ Python36 \ lastmainprogram.py”,行74,在moving_avg = pd.rolling_mean(ts_log,12)中AttributeError:模块’pan

  • 我正在尝试为一个名为Zulip的开源项目构建python api,我一直遇到下面截图所示的相同问题。 我正在运行python3,我的pip版本是10.0。0.正在讨论的文件是而混乱的代码是当属性可用于安装软件包。 现在,我知道这个构建应该成功,因为它是一个开源项目,但是我已经尝试了几个小时来修复关于的依赖问题。 任何帮助将不胜感激。

  • 我正在尝试使用Tensorflow对象检测API来训练我自己的自定义对象检测器 我在谷歌计算引擎中使用“pip install tensorflow”安装了tensorflow。然后,我按照本网站上的所有说明进行操作:https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/training.html 当我尝

  • 我得到的错误,而不是安装所有的依赖,如python3.6,Numpy,opencv,open-contrib-python。 我试过升级opencv 错误消息 模块cv2.cv2没有属性

  • 我正在创建一个非常基本的python程序,将它与psql中的数据库连接起来。 我的代码如下: 从sqlalchemy导入创建引擎 sqlalchemy.orm进口scoped_session 引擎=创建引擎(“postgresql psycopg2://sidrules:password@localhost:5432/第一个“) db=作用域_会话(sessionmaker(bind=engine