You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pyrogram下载Telegram图片时无法捕获FloodWait异常求助

Pyrogram下载Telegram图片时无法捕获FloodWait异常

使用Pyrogram API批量下载Telegram图片时,触发限流后,自定义的FloodWait异常捕获代码完全没触发,程序继续循环报错,根本不执行等待逻辑。怀疑是download_media()内部提前捕获了异常,导致外层的try-catch没生效,连异常信息都打不出来。

报错信息

Telegram says: [420 FLOOD_WAIT_X] - A wait of 1219 seconds is required (caused by "auth.ExportAuthorization")
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/pyrogram/client.py", line 880, in get_file
    dc_id=dc_id
  File "/usr/local/lib/python3.7/dist-packages/pyrogram/methods/advanced/invoke.py", line 83, in invoke
    else self.sleep_threshold)
  File "/usr/local/lib/python3.7/dist-packages/pyrogram/session/session.py", line 389, in invoke
    return await self.send(query, timeout=timeout)
  File "/usr/local/lib/python3.7/dist-packages/pyrogram/session/session.py", line 357, in send
    RPCError.raise_it(result, type(data))
  File "/usr/local/lib/python3.7/dist-packages/pyrogram/errors/rpc_error.py", line 97, in raise_it
    is_signed=is_signed)
pyrogram.errors.exceptions.flood_420.FloodWait: Telegram says: [420 FLOOD_WAIT_X] - A wait of 1219 seconds is required (caused by `"auth.ExportAuthorization")

当前代码

try:
    downloaded_file = await app.download_media(new_message.photo, file_name=new_file_path, progress=progress)
except FloodWait as e:
    print("Now waiting seconds", e.value)
    await asyncio.sleep(e.value)  # Wait "value" seconds before continuing

修复方案

1. 确认异常导入路径

确保正确从pyrogram.errors导入FloodWait,避免导入错误导致捕获失败:

from pyrogram.errors import FloodWait

2. 拆分下载流程,直接捕获底层异常

download_media内部封装了get_file和download步骤,部分异常可能在内部被处理后未抛出。手动拆分这两个步骤,直接捕获get_file阶段的FloodWait:

from pyrogram.errors import FloodWait
import asyncio

# 替换原有的download_media调用
try:
    # 先获取文件元数据
    file_obj = await app.get_file(new_message.photo)
    # 执行实际下载
    downloaded_file = await app.download(file_obj, file_name=new_file_path, progress=progress)
except FloodWait as e:
    print(f"触发限流,需要等待 {e.value} 秒")
    await asyncio.sleep(e.value)
    # 等待后重新尝试
    file_obj = await app.get_file(new_message.photo)
    downloaded_file = await app.download(file_obj, file_name=new_file_path, progress=progress)

3. 循环外层添加全局异常捕获

如果单条消息的try-catch仍失效,在循环外层添加全局捕获,确保异常被拦截并处理:

async def batch_download(messages):
    for msg in messages:
        try:
            downloaded_file = await app.download_media(msg.photo, file_name=new_file_path, progress=progress)
        except FloodWait as e:
            print(f"等待 {e.value} 秒后重试当前图片")
            await asyncio.sleep(e.value)
            # 重试当前消息
            downloaded_file = await app.download_media(msg.photo, file_name=new_file_path, progress=progress)
        except Exception as e:
            print(f"下载失败: {str(e)}")
            continue

内容的提问来源于stack exchange,提问作者yuskox

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 06:05:42