对示例程序spinner_asyncio.py进行修改使其能运行

学习《流畅的python》第18章 使用asyncio包处理并发,运行示例18-2 spinner_asyncio.py的时候,程序报错如下:

D:\fluentPy\chapter17>python spinner_asyncio.py

File "D:\fluentPy\chapter17\spinner_asyncio.py", line 30

spinner = asyncio.async(spin('thinking!'))

^^^^^

SyntaxError: invalid syntax

其实在PyCharm中已经报错了,因为我的环境python311不支持async函数了。在https://docs.python.org/上查到async函数已经被ensure_future函数替代了。

asyncio.async(coro_or_future, *, loop=None)

A deprecated alias to ensure_future().

Deprecated since version 3.4.4.

改过之后运行程序,还是报错:

D:\fluentPy\chapter17>python spinner_asyncio.py

Traceback (most recent call last):

File "D:\fluentPy\chapter17\spinner_asyncio.py", line 6, in <module>

@asyncio.coroutine

^^^^^^^^^^^^^^^^^

AttributeError: module 'asyncio' has no attribute 'coroutine'. Did you mean: 'coroutines'?

我把python降级到python 3.7.9,问题终于解决:

D:\fluentPy\chapter17>"c:\Program Files\Python37\python" spinner_asyncio.py

spinner object: <Task pending coro=<spin() running at spinner_asyncio.py:6>>

Answer: 42

相关推荐
万粉变现经纪人2 小时前
如何解决 pip install 安装报错 ModuleNotFoundError: No module named ‘tokenizers’ 问题
python·selenium·测试工具·scrapy·beautifulsoup·fastapi·pip
编程武士4 小时前
从50ms到30ms:YOLOv10部署中图像预处理的性能优化实践
人工智能·python·yolo·性能优化
我的xiaodoujiao4 小时前
Windows系统Web UI自动化测试学习系列2--环境搭建--Python-PyCharm-Selenium
开发语言·python·测试工具
傻啦嘿哟7 小时前
Python SQLite模块:轻量级数据库的实战指南
数据库·python·sqlite
Q_Q5110082857 小时前
python+django/flask+uniapp基于微信小程序的瑜伽体验课预约系统
spring boot·python·django·flask·uni-app·node.js·php
XueminXu7 小时前
Python读取MongoDB的JSON字典和列表对象转为字符串
python·mongodb·json·pymongo·mongoclient·isinstance·json.dumps
techdashen7 小时前
12分钟讲解Python核心理念
开发语言·python
jie*7 小时前
小杰机器学习(nine)——支持向量机
人工智能·python·机器学习·支持向量机·回归·聚类·sklearn
闭着眼睛学算法7 小时前
【华为OD机考正在更新】2025年双机位A卷真题【完全原创题解 | 详细考点分类 | 不断更新题目 | 六种主流语言Py+Java+Cpp+C+Js+Go】
java·c语言·javascript·c++·python·算法·华为od
郝学胜-神的一滴7 小时前
谨慎地迭代函数所收到的参数 (Effective Python 第31条)
开发语言·python·程序人生·软件工程