修改例程flags3_asyncio.py以解决RuntimeError错误

运行示例18-13 flags3_asyncio.py,程序报错如下:

D:\Python\Python311\python.exe D:\fluentPy\chapter17_18\flags3_asyncio.py

REMOTE site: https://www.fluentpython.com/data/flags/

Searching for 20 flags: from BD to VN

5 concurrent connections will be used.

100%|██████████| 20/20 [00:07<00:00, 2.61it/s]


20 flags downloaded.

Elapsed time: 7.70s

exception calling callback for <Future at 0x1bc2da71b90 state=finished returned NoneType>

Traceback (most recent call last):

......

raise RuntimeError('Event loop is closed')

RuntimeError: Event loop is closed

进程已结束,退出代码为 0

很奇怪,明明下载成功了,但是最后还是出现错误提示,经过检查,发现问题出在函数

async def download_one(cc, base_url, semaphore, verbose):

try:

async with semaphore:

image = await get_flag(base_url, cc)

async with semaphore:

country = await get_country(base_url, cc)

except web.HTTPNotFound:

status = HTTPStatus.not_found

msg = 'not found'

except Exception as exc:

raise FetchError(cc) from exc

else:

country = country.replace(' ', '_')

filename = '{}-{}.gif'.format(country, cc)

loop = asyncio.get_event_loop()

loop.run_in_executor(None, save_flag, image, filename)

status = HTTPStatus.ok

msg = 'OK'

if verbose and msg:

print(cc, msg)

return Result(status, cc)

里,只要把loop = asyncio.get_event_loop()改成loop = asyncio.new_event_loop()就好了。

相关推荐
少废话h44 分钟前
解决Flink中ApacheCommonsCLI版本冲突
开发语言·python·pycharm
serve the people1 小时前
TensorFlow 图执行(tf.function)的 “非严格执行(Non-strict Execution)” 特性
人工智能·python·tensorflow
天命码喽c1 小时前
GraphRAG-2.7.0整合Milvus-2.5.1
开发语言·python·milvus·graphrag
吴佳浩3 小时前
LangChain 深入
人工智能·python·langchain
网安-轩逸6 小时前
回归测试原则:确保软件质量的基石
自动化测试·软件测试·python
Mr_Xuhhh6 小时前
YAML相关
开发语言·python
咖啡の猫6 小时前
Python中的变量与数据类型
开发语言·python
汤姆yu6 小时前
基于springboot的电子政务服务管理系统
开发语言·python
执笔论英雄7 小时前
【RL】python协程
java·网络·人工智能·python·设计模式
帮帮志8 小时前
【AI大模型对话】流式输出和非流式输出的定义和区别
开发语言·人工智能·python·大模型·anaconda