为了账号安全,请及时绑定邮箱和手机立即绑定

Django StreamingHttpResponse() 导致服务器停止工作

Django StreamingHttpResponse() 导致服务器停止工作

慕标5832272 2023-12-08 16:01:55
我正在尝试实现 StreamingHttpResponse 但遇到了一个繁琐的问题。连接似乎已建立,然后在大约 2 个页面请求后,网络服务器停止响应。我真的很困惑是什么导致了这个问题。如果它与无限循环相关联,则 time.sleep() 方法应该可以防止服务器立即过载。我将不胜感激任何帮助,谢谢!views.py:    def event_stream():    initial_data = ""    while True:        data = json.dumps(list(Notification.objects.filter(to_user=1).order_by("-created_date").values("info",        "return_url",        "from_user","to_user","created_date")),        cls=DjangoJSONEncoder)        if not initial_data == data:            yield "\ndata: {}\n\n".format(data)            initial_data = data        time.sleep(1)class PostStreamView(View):    def get(self, request):        response = StreamingHttpResponse(event_stream())        response['Content-Type'] = 'text/event-stream'        return response基本.htmlvar eventSource  = new EventSource("{% url 'stream' %}");eventSource.onopen = function() {    console.log('We have open connection!');  }  eventSource.onmessage = function(e) {    console.log(e)  }  eventSource.onerror = function(e) {    console.log(`error ${e}`);  }</script>服务器日志:2020-10-20 19:25:51 Tue Oct 20 19:25:51 2020 - SIGPIPE: writing to a closed pipe/socket/fd (probably the client disconnected) on request /AP%20Psychology/ (ip 10.0.0.124) !!!2020-10-20 19:25:51 Tue Oct 20 19:25:51 2020 - uwsgi_response_writev_headers_and_body_do(): Broken pipe [core/writer.c line 306] during GET /AP%20Psychology/ (10.0.0.124)2020-10-20 19:27:33 Tue Oct 20 19:27:33 2020 - SIGPIPE: writing to a closed pipe/socket/fd (probably the client disconnected) on request /post/12/ (ip 10.0.0.124) !!!2020-10-20 19:27:33 Tue Oct 20 19:27:33 2020 - uwsgi_response_writev_headers_and_body_do(): Broken pipe [core/writer.c line 306] during GET /post/12/ (10.0.0.124)我的托管提供商是 PythonAnywhere
查看完整描述

1 回答

?
慕侠2389804

TA贡献1719条经验 获得超6个赞

这里可能会发生一些事情。SIGPIPE 错误表明浏览器正在关闭连接或连接超时(PythonAnywhere 上的打开连接有 5 分钟限制)当连接打开超过 5 分钟时,您的 Web 应用程序可能会重新启动,因此有些代码会在 5 分钟限制之前中断连接并重建连接,这样可以防止这种情况发生。另一种可能性是你的工人已经用完了。每个流都会占用一名工作人员,无论其打开时间有多长,并且您至少需要一名工作人员来处理普通请求。



查看完整回答
反对 回复 2023-12-08
  • 1 回答
  • 0 关注
  • 71 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信