修改例程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()就好了。

相关推荐
Eric.Lee20218 分钟前
python opencv 将不同shape尺寸的图片制作video视频
python·opencv·音视频
Amo Xiang19 分钟前
Python 常用模块(八):logging模块
python·logging·日志
森哥的歌20 分钟前
Python多线程
python·编程·多线程·并发·threading
抽风的雨61043 分钟前
【python基础知识】Day26 函数
开发语言·python
编程有点难1 小时前
Python训练打卡Day22
开发语言·python·机器学习
天机️灵韵2 小时前
字节开源FlowGram与n8n 技术选型
人工智能·python·开源项目
兮兮能吃能睡2 小时前
Python之with语句
数据库·python
boooo_hhh2 小时前
第28周——InceptionV1实现猴痘识别
python·深度学习·机器学习
咕噜咕噜啦啦2 小时前
Python爬虫入门
开发语言·爬虫·python
dubochao_xinxi2 小时前
✅ TensorRT Python 安装精简流程(适用于 Ubuntu 20.04+)
开发语言·python·ubuntu