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

相关推荐
查理零世1 小时前
保姆级讲解 python之zip()方法实现矩阵行列转置
python·算法·矩阵
刀客1231 小时前
python3+TensorFlow 2.x(四)反向传播
人工智能·python·tensorflow
sysu632 小时前
95.不同的二叉搜索树Ⅱ python
开发语言·数据结构·python·算法·leetcode·面试·深度优先
SsummerC2 小时前
【leetcode100】从前序与中序遍历序列构造二叉树
python·算法·leetcode
陌北v13 小时前
PyTorch广告点击率预测(CTR)利用深度学习提升广告效果
人工智能·pytorch·python·深度学习·ctr
Мартин.3 小时前
[Meachines] [Easy] Bashed PHP Bash+Python计划任务权限提升
python·php·bash
码界筑梦坊4 小时前
基于Flask的旅游系统的设计与实现
python·flask·毕业设计·旅游
辞落山4 小时前
自定义数据集使用scikit-learn中的包实现线性回归方法对其进行拟合
python·线性回归·scikit-learn
Allen200005 小时前
wow-agent---task4 MetaGPT初体验
人工智能·python·pygame
源代码杀手8 小时前
【以音频软件FFmpeg为例】通过Python脚本将软件路径添加到Windows系统环境变量中的实现与原理分析
windows·python·音视频