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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值