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

如何解决Telethon发送大文件时的RPCError -503超时问题

解决SaveBigFilePartRequest超时(RPCError -503)的方案

针对你使用Telethon发送大文件时频繁遇到的RPCError -503: Timedout (caused by SaveBigFilePartRequest)错误,除了自动重试,可通过以下修改从根源减少或避免该问题:

  • 调整文件分片大小
    默认分片体积过大可能导致单块上传超时,手动设置chunk_size参数缩小分片,推荐设置为128KB或256KB(根据网络情况调整)。修改后的代码:

    await client.send_file(
        channel_id, 
        file_name, 
        thumb=thumbnail, 
        caption=caption, 
        attributes=[types.DocumentAttributeVideo(duration=video_duration, w=0, h=0, supports_streaming=True)], 
        mime_type='video/mp4',
        chunk_size=256*1024  # 设置256KB分片
    )
    
  • 延长请求超时时间
    增大客户端或请求的超时阈值,给大文件分片足够的上传时间。可在初始化客户端时全局设置,或在send_file中单独指定:

    # 初始化客户端时设置全局超时(单位:秒)
    client = TelegramClient(session, api_id, api_hash, timeout=120)
    
    # 或在send_file中单独设置
    await client.send_file(
        # 其他参数...
        timeout=120
    )
    
  • 优化网络与API环境

    • 关闭不必要的代理,或切换到更稳定的网络环境;
    • 若使用自托管Telegram API服务器,检查服务器带宽、负载,确保能支撑大文件分片上传需求。
  • 手动预上传文件(进阶)
    先通过upload_file方法将文件上传至Telegram服务器,得到文件对象后再发送,可更精细控制上传流程:

    from telethon.tl.types import InputFile
    
    # 手动上传文件,自定义分片和超时
    file_obj = await client.upload_file(
        file_name,
        chunk_size=256*1024,
        timeout=120
    )
    
    # 发送已上传的文件对象
    await client.send_file(
        channel_id, 
        file_obj, 
        thumb=thumbnail, 
        caption=caption, 
        attributes=[types.DocumentAttributeVideo(duration=video_duration, w=0, h=0, supports_streaming=True)], 
        mime_type='video/mp4'
    )
    
  • 智能重试策略
    替换简单重试为指数退避重试,避免短时间内重复请求加剧服务器压力:

    import time
    from telethon.errors import RPCError
    
    max_retries = 5
    retry_delay = 1  # 初始重试间隔1秒
    
    for attempt in range(max_retries):
        try:
            await client.send_file(
                channel_id, 
                file_name, 
                thumb=thumbnail, 
                caption=caption, 
                attributes=[types.DocumentAttributeVideo(duration=video_duration, w=0, h=0, supports_streaming=True)], 
                mime_type='video/mp4',
                chunk_size=256*1024,
                timeout=120
            )
            break
        except RPCError as e:
            if e.code == -503 and attempt < max_retries - 1:
                time.sleep(retry_delay)
                retry_delay *= 2  # 指数延长间隔
            else:
                raise  # 超过重试次数则抛出错误
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 08:43:28