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

如何避免Python导出大型CSV时超时?(Django+Azure场景)

问题描述

我在项目中需要实现大型数据库的CSV导出功能,Django项目部署在Azure平台,但每次执行导出操作都会触发超时错误。我尝试通过异步函数解决该问题,已经试过async_to_sync、sync_to_async、启动Daphne服务器以及StreamingHttpResponse等方案,以下是我的实现代码:

class Echo:
    """An object that implements just the write method of the file-like
    interface.
    """

    def write(self, value):
        """Write the value by returning it, instead of storing in a buffer."""
        return value


@api_view(['POST'])
@permission_classes((permissions.IsAuthenticated,))
def export_csv(request):
    try:
        # 耗时任务
        first_item, second_item, third_item = my_function(request.data)

        csv_tab = []
        csv_tab.append(build_headers())

        # 耗时任务
        for item in first_item:
            csv_tab.extend(build_lines(item, second_item, third_item))

        pseudo_buffer = Echo()

        writer = csv.writer(pseudo_buffer, delimiter=';')

        response = StreamingHttpResponse(
            (writer.writerow(line) for line in csv_tab),
            content_type='text/csv',
            headers={
                'Content-Disposition': 'attachment; filename="somefilename.csv"'},
        )

        return response
    except Exception as identifier:
        LOGGER.exception(identifier)
        current = {
            'state': "failure",
            'reason': "server error",
        }
        print(identifier)
        return HttpResponse(json.dumps(current), status=500)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 08:54:23