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

相关推荐
reasonsummer43 分钟前
【办公类-100-01】20250515手机导出教学照片,自动上传csdn+最大化、最小化Vs界面
开发语言·python
Doker 多克1 小时前
Python-Django系列—日志
python·日志
苏三福2 小时前
ros2 hunmle bag 数据包转为图片数据 python版
开发语言·python·ros2humble
大神薯条老师3 小时前
Python零基础入门到高手8.4节: 元组与列表的区别
开发语言·爬虫·python·深度学习·机器学习·数据分析
z人间防沉迷k3 小时前
堆(Heap)
开发语言·数据结构·笔记·python·算法
小白学大数据4 小时前
Python爬虫如何应对网站的反爬加密策略?
开发语言·爬虫·python
Eric.Lee20214 小时前
Ubuntu shell指定conda的python环境启动脚本
python·ubuntu·conda·shell
芒果量化4 小时前
量化交易 - 网格交易策略实现与原理解析
python·算法·机器学习·金融
LetsonH4 小时前
Python工具链UV整合环境管理
开发语言·python·uv
欣然~4 小时前
基于深度学习进行运输系统优化
python·深度学习