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

相关推荐
guhy fighting3 分钟前
pycharm 切换版本和窗口cmd看到的版本不一致问题解决
ide·python·pycharm
今儿敲了吗13 分钟前
python基础学习笔记第六章——函数进阶
笔记·python·学习
码码哈哈0.043 分钟前
LangChain 快速入门(从0到可用)
开发语言·python·langchain
2301_776508721 小时前
用Python生成艺术:分形与算法绘图
jvm·数据库·python
熊文豪1 小时前
Java 入门指南
开发语言·python
带娃的IT创业者1 小时前
WeClaw 日志分析实战:如何从海量日志中快速定位根因?
运维·python·websocket·jenkins·fastapi·架构设计·实时通信
wh_xia_jun2 小时前
MODNet 本地抠图项目指南
python
老歌老听老掉牙3 小时前
Python星号参数深度解析
python·参数·星号
2401_884563243 小时前
Python Lambda(匿名函数):简洁之道
jvm·数据库·python