我正在尝试实现 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
基本.html
var 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
慕侠2389804
相关分类