对示例程序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

相关推荐
冷月半明5 分钟前
trea solo,让我从牛马外包翻身当“甲方”
python·trae
superman超哥27 分钟前
仓颉借用检查器工作原理深度解析
c语言·开发语言·c++·python·仓颉
张彦峰ZYF42 分钟前
Python 项目文件组织与工程化实践
python·项目文件组织与工程化实践
webbodys1 小时前
Python文件操作与异常处理:构建健壮的应用程序
java·服务器·python
MediaTea2 小时前
Python:实例 __dict__ 详解
java·linux·前端·数据库·python
SunnyDays10113 小时前
Python Excel 打印设置全攻略(打印区域、缩放、页边距、页眉页脚等)
python·excel打印设置·excel页面设置·excel打印选项
小鸡吃米…3 小时前
Python的人工智能-机器学习
人工智能·python·机器学习
傻啦嘿哟3 小时前
Python上下文管理器:优雅处理资源释放的魔法工具
开发语言·python
阿方索3 小时前
Python 基础简介
开发语言·python
BBB努力学习程序设计3 小时前
Python异步编程完全指南:从asyncio到高性能应用
python·pycharm