我正在嘗試實現(xiàn) StreamingHttpResponse 但遇到了一個繁瑣的問題。連接似乎已建立,然后在大約 2 個頁面請求后,網(wǎng)絡(luò)服務(wù)器停止響應(yīng)。我真的很困惑是什么導(dǎo)致了這個問題。如果它與無限循環(huán)相關(guān)聯(lián),則 time.sleep() 方法應(yīng)該可以防止服務(wù)器立即過載。我將不勝感激任何幫助,謝謝!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>服務(wù)器日志: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
Django StreamingHttpResponse() 導(dǎo)致服務(wù)器停止工作
慕標(biāo)5832272
2023-12-08 16:01:55