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

相关推荐
Mr_Xuhhh2 小时前
GUI自动化测试--自动化测试的意义和应用场景
python·集成测试
2301_764441332 小时前
水星热演化核幔耦合数值模拟
python·算法·数学建模
循环过三天2 小时前
3.4、Python-集合
开发语言·笔记·python·学习·算法
Q_Q5110082853 小时前
python+django/flask的眼科患者随访管理系统 AI智能模型
spring boot·python·django·flask·node.js·php
SunnyDays10115 小时前
如何使用Python高效转换Excel到HTML
python·excel转html
Q_Q5110082855 小时前
python+django/flask的在线学习系统的设计与实现 积分兑换礼物
spring boot·python·django·flask·node.js·php
Q_Q5110082856 小时前
python+django/flask的车辆尾气检测排放系统-可视化大屏展示
spring boot·python·django·flask·node.js·php
汤姆yu6 小时前
2026版基于python大数据的旅游可视化及推荐系统
python·旅游·大数据旅游
angleoldhen6 小时前
简单的智能数据分析程序
python·信息可视化·数据分析
youzj09257 小时前
docker网站配置
python