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

将S3兼容文件转发至Flask响应时遇调度错误求助

问题分析与解决方案

错误原因

RuntimeError: cannot schedule new futures after interpreter shutdown 通常出现在异步Web环境中使用同步S3客户端方法的场景:同步的download_fileobj会阻塞事件循环,导致主线程/解释器提前终止,而后续异步任务仍在调度,引发错误。调整后请求挂起则是因为将整个文件加载到内存后才返回响应,大文件会导致等待时间过长,甚至阻塞进程。

解决方案

1. 改用异步S3客户端

使用aioboto3替代同步boto3,确保所有S3操作与异步环境兼容,避免阻塞事件循环。

2. 流式传输文件

不要将整个文件加载到内存,直接将S3文件流传递给响应对象,实现边下载边返回,解决请求挂起问题。

代码示例

安装依赖

pip install aioboto3

异步读取S3文件函数

import aioboto3
from botocore.exceptions import ClientError

async def read_file(project_id, path, defaultValue=None):
    try:
        session = aioboto3.Session()
        # 替换为你的S3兼容服务器地址
        async with session.client('s3', endpoint_url='https://your-s3-compatible-server.com') as client:
            # 获取文件流,无需全部加载到内存
            s3_response = await client.get_object(Bucket=project_id, Key=path)
            return s3_response['Body']
    except ClientError as e:
        if defaultValue is not None:
            return defaultValue
        else:
            raise S3Error(project_id, path, e) from e

生成响应(以FastAPI为例)

from fastapi.responses import StreamingResponse

async def download_data():
    file_stream = await read_file(project_id, f"{PATH}/data.csv")
    return StreamingResponse(
        file_stream,
        media_type="application/octet-stream; charset=utf-8",
        headers={
            "Content-Disposition": "attachment; filename=data.csv"
        }
    )

生成响应(以Flask异步视图为例)

from flask import Response

async def download_data():
    file_stream = await read_file(project_id, f"{PATH}/data.csv")
    
    async def stream_generator():
        # 分块读取并返回数据
        async for chunk in file_stream.iter_chunks(chunk_size=8192):
            yield chunk
    
    return Response(
        stream_generator(),
        mimetype="application/octet-stream; charset=utf-8",
        headers={
            "Content-Disposition": "attachment; filename=data.csv"
        }
    )

关键说明

  • 异步客户端确保S3操作不会阻塞事件循环,避免解释器提前shutdown的错误。
  • 流式传输无需等待整个文件下载完成即可返回响应,彻底解决请求挂起问题,同时降低内存占用。

内容的提问来源于stack exchange,提问作者Edmund's Echo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 04:15:42