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

Django StreamingHttpResponse流式响应异常问题咨询

问题解决:Django StreamingHttpResponse 无法实时返回数据块

核心原因

服务器端的输出缓冲机制导致生成器产生的小数据块被暂时存储,直到缓冲填满或生成器迭代结束才一次性发送给客户端。你看到服务器每0.1秒打印一次,说明生成器正常工作,但数据被拦截在服务器缓冲层。

具体解决办法

1. 禁用开发服务器的响应缓冲(仅开发环境)

Django默认的runserver在DEBUG模式下会缓冲响应,可通过自定义WSGI服务器关闭缓冲:

from django.core.wsgi import get_wsgi_application
from wsgiref.simple_server import make_server
import os

os.environ.setdefault('DJANGO_SETTINGS_MODULE', '你的项目名.settings')
application = get_wsgi_application()

class UnbufferedWSGIHandler(application.__class__):
    def __call__(self, environ, start_response):
        response = super().__call__(environ, start_response)
        if hasattr(response, '__iter__'):
            environ['wsgi.file_wrapper'] = lambda f, blksize=1024: f
        return response

application = UnbufferedWSGIHandler()

if __name__ == '__main__':
    httpd = make_server('0.0.0.0', 8000, application)
    httpd.serve_forever()

用这个脚本替代python manage.py runserver启动服务。

2. 调整生产服务器配置(以Gunicorn为例)

默认的sync worker会缓冲流式响应,需改用异步worker:

gunicorn --worker-class=gevent --bind 0.0.0.0:8000 你的项目名.wsgi:application

或添加--no-sendfile参数禁用系统级缓冲。

3. 优化响应头设置

确保响应头明确禁用缓存并声明分块传输:

def post(self, request):
    stream = iterator()
    response = StreamingHttpResponse(stream, status=200, content_type='text/event-stream')
    response['Cache-Control'] = 'no-cache, no-store'
    response['Transfer-Encoding'] = 'chunked'
    # 移除可能自动添加的Content-Length头
    if 'Content-Length' in response:
        del response['Content-Length']
    return response

4. 强制触发缓冲发送

部分服务器需要数据块达到一定大小才会推送,可在每个数据块后补充空白字符:

def gen_message(msg):
    # 添加足够空白触发缓冲刷新
    return f'\ndata: {msg}\n\n' + ' ' * 1024

5. 调整客户端读取逻辑

修改requests的iter_lines参数,确保实时读取:

r = requests.post('https://******/test_stream/', stream=True)

for line in r.iter_lines(chunk_size=1):
    if line:
        decoded_line = line.decode('utf-8')
        print(decoded_line)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 14:50:16