尝试在Django Rest Framwork视图中尝试发出socketio消息有时失败

问题描述 投票:0回答:1

[在某些情况下,我的Django Rest Framework应用程序需要使用socketio向另一台服务器发送通知。发出通知的功能包含在views.py文件中,并在需要时直接从Rest请求处理程序中调用。

在大多数情况下,它都可以正常工作,但是有时调用emit()会引发异常。到目前为止,至少发生了三种不同的异常。

该应用程序在Elastic Beanstalk上运行。

发出消息的代码:

def send_request(message):
    sio = socketio.Client()

    @sio.event
    def connect():
        sio.emit(settings.KEY, message) # Exception happens on this line, if it happens at all
        sio.wait()
        sio.disconnect()

    sio.connect(settings.URL,transports='websocket')

来自以下三个不同错误的日志:

[Sun Feb 09 15:36:44.732110 2020] [:error] [pid 1713] Exception in thread Thread-262167:
[Sun Feb 09 15:36:44.732131 2020] [:error] [pid 1713] Traceback (most recent call last):
[Sun Feb 09 15:36:44.732133 2020] [:error] [pid 1713]   File "/usr/lib64/python3.6/threading.py", line 916, in _bootstrap_inner
[Sun Feb 09 15:36:44.732136 2020] [:error] [pid 1713]     self.run()
[Sun Feb 09 15:36:44.732138 2020] [:error] [pid 1713]   File "/usr/lib64/python3.6/threading.py", line 864, in run
[Sun Feb 09 15:36:44.732141 2020] [:error] [pid 1713]     self._target(*self._args, **self._kwargs)
[Sun Feb 09 15:36:44.732143 2020] [:error] [pid 1713]   File "/opt/python/run/venv/local/lib/python3.6/site-packages/engineio/client.py", line 665, in _write_loop
[Sun Feb 09 15:36:44.732145 2020] [:error] [pid 1713]     self.ws.send(encoded_packet)
[Sun Feb 09 15:36:44.732148 2020] [:error] [pid 1713]   File "/opt/python/run/venv/local/lib/python3.6/site-packages/websocket/_core.py", line 253, in send
[Sun Feb 09 15:36:44.732150 2020] [:error] [pid 1713]     return self.send_frame(frame)
[Sun Feb 09 15:36:44.732152 2020] [:error] [pid 1713]   File "/opt/python/run/venv/local/lib/python3.6/site-packages/websocket/_core.py", line 278, in send_frame
[Sun Feb 09 15:36:44.732154 2020] [:error] [pid 1713]     l = self._send(data)
[Sun Feb 09 15:36:44.732156 2020] [:error] [pid 1713]   File "/opt/python/run/venv/local/lib/python3.6/site-packages/websocket/_core.py", line 448, in _send
[Sun Feb 09 15:36:44.732159 2020] [:error] [pid 1713]     return send(self.sock, data)
[Sun Feb 09 15:36:44.732161 2020] [:error] [pid 1713]   File "/opt/python/run/venv/local/lib/python3.6/site-packages/websocket/_socket.py", line 157, in send
[Sun Feb 09 15:36:44.732163 2020] [:error] [pid 1713]     return _send()
[Sun Feb 09 15:36:44.732166 2020] [:error] [pid 1713]   File "/opt/python/run/venv/local/lib/python3.6/site-packages/websocket/_socket.py", line 139, in _send
[Sun Feb 09 15:36:44.732168 2020] [:error] [pid 1713]     return sock.send(data)
[Sun Feb 09 15:36:44.732170 2020] [:error] [pid 1713]   File "/usr/lib64/python3.6/ssl.py", line 946, in send
[Sun Feb 09 15:36:44.732172 2020] [:error] [pid 1713]     return socket.send(self, data, flags)
[Sun Feb 09 15:36:44.732174 2020] [:error] [pid 1713] OSError: [Errno 9] Bad file descriptor

[Sun Feb 09 15:36:36.556780 2020] [:error] [pid 1713] Exception in thread Thread-257085:
[Sun Feb 09 15:36:36.557014 2020] [:error] [pid 1713] Traceback (most recent call last):
[Sun Feb 09 15:36:36.557030 2020] [:error] [pid 1713]   File "/usr/lib64/python3.6/threading.py", line 916, in _bootstrap_inner
[Sun Feb 09 15:36:36.557037 2020] [:error] [pid 1713]     self.run()
[Sun Feb 09 15:36:36.557040 2020] [:error] [pid 1713]   File "/usr/lib64/python3.6/threading.py", line 864, in run
[Sun Feb 09 15:36:36.557044 2020] [:error] [pid 1713]     self._target(*self._args, **self._kwargs)
[Sun Feb 09 15:36:36.557047 2020] [:error] [pid 1713]   File "/opt/python/run/venv/local/lib/python3.6/site-packages/engineio/client.py", line 665, in _write_loop
[Sun Feb 09 15:36:36.557063 2020] [:error] [pid 1713]     self.ws.send(encoded_packet)
[Sun Feb 09 15:36:36.557066 2020] [:error] [pid 1713]   File "/opt/python/run/venv/local/lib/python3.6/site-packages/websocket/_core.py", line 253, in send
[Sun Feb 09 15:36:36.557068 2020] [:error] [pid 1713]     return self.send_frame(frame)
[Sun Feb 09 15:36:36.557070 2020] [:error] [pid 1713]   File "/opt/python/run/venv/local/lib/python3.6/site-packages/websocket/_core.py", line 278, in send_frame
[Sun Feb 09 15:36:36.557072 2020] [:error] [pid 1713]     l = self._send(data)
[Sun Feb 09 15:36:36.557074 2020] [:error] [pid 1713]   File "/opt/python/run/venv/local/lib/python3.6/site-packages/websocket/_core.py", line 448, in _send
[Sun Feb 09 15:36:36.557076 2020] [:error] [pid 1713]     return send(self.sock, data)
[Sun Feb 09 15:36:36.557078 2020] [:error] [pid 1713]   File "/opt/python/run/venv/local/lib/python3.6/site-packages/websocket/_socket.py", line 157, in send
[Sun Feb 09 15:36:36.557081 2020] [:error] [pid 1713]     return _send()
[Sun Feb 09 15:36:36.557083 2020] [:error] [pid 1713]   File "/opt/python/run/venv/local/lib/python3.6/site-packages/websocket/_socket.py", line 139, in _send
[Sun Feb 09 15:36:36.557085 2020] [:error] [pid 1713]     return sock.send(data)
[Sun Feb 09 15:36:36.557087 2020] [:error] [pid 1713]   File "/usr/lib64/python3.6/ssl.py", line 944, in send
[Sun Feb 09 15:36:36.557089 2020] [:error] [pid 1713]     return self._sslobj.write(data)
[Sun Feb 09 15:36:36.557091 2020] [:error] [pid 1713]   File "/usr/lib64/python3.6/ssl.py", line 642, in write
[Sun Feb 09 15:36:36.557093 2020] [:error] [pid 1713]     return self._sslobj.write(data)
[Sun Feb 09 15:36:36.557095 2020] [:error] [pid 1713] BrokenPipeError: [Errno 32] Broken pipe

[Thu Feb 06 17:52:28.818718 2020] [:error] [pid 1713] Exception in thread Thread-252469:
[Thu Feb 06 17:52:28.818740 2020] [:error] [pid 1713] Traceback (most recent call last):
[Thu Feb 06 17:52:28.818743 2020] [:error] [pid 1713]   File "/usr/lib64/python3.6/threading.py", line 916, in _bootstrap_inner
[Thu Feb 06 17:52:28.818746 2020] [:error] [pid 1713]     self.run()
[Thu Feb 06 17:52:28.818749 2020] [:error] [pid 1713]   File "/usr/lib64/python3.6/threading.py", line 864, in run
[Thu Feb 06 17:52:28.818751 2020] [:error] [pid 1713]     self._target(*self._args, **self._kwargs)
[Thu Feb 06 17:52:28.818754 2020] [:error] [pid 1713]   File "/opt/python/run/venv/local/lib/python3.6/site-packages/engineio/client.py", line 665, in _write_loop
[Thu Feb 06 17:52:28.818756 2020] [:error] [pid 1713]     self.ws.send(encoded_packet)
[Thu Feb 06 17:52:28.818759 2020] [:error] [pid 1713]   File "/opt/python/run/venv/local/lib/python3.6/site-packages/websocket/_core.py", line 253, in send
[Thu Feb 06 17:52:28.818761 2020] [:error] [pid 1713]     return self.send_frame(frame)
[Thu Feb 06 17:52:28.818763 2020] [:error] [pid 1713]   File "/opt/python/run/venv/local/lib/python3.6/site-packages/websocket/_core.py", line 278, in send_frame
[Thu Feb 06 17:52:28.818765 2020] [:error] [pid 1713]     l = self._send(data)
[Thu Feb 06 17:52:28.818767 2020] [:error] [pid 1713]   File "/opt/python/run/venv/local/lib/python3.6/site-packages/websocket/_core.py", line 448, in _send
[Thu Feb 06 17:52:28.818769 2020] [:error] [pid 1713]     return send(self.sock, data)
[Thu Feb 06 17:52:28.818771 2020] [:error] [pid 1713]   File "/opt/python/run/venv/local/lib/python3.6/site-packages/websocket/_socket.py", line 157, in send
[Thu Feb 06 17:52:28.818782 2020] [:error] [pid 1713]     return _send()
[Thu Feb 06 17:52:28.818785 2020] [:error] [pid 1713]   File "/opt/python/run/venv/local/lib/python3.6/site-packages/websocket/_socket.py", line 139, in _send
[Thu Feb 06 17:52:28.818787 2020] [:error] [pid 1713]     return sock.send(data)
[Thu Feb 06 17:52:28.818789 2020] [:error] [pid 1713]   File "/usr/lib64/python3.6/ssl.py", line 944, in send
[Thu Feb 06 17:52:28.818791 2020] [:error] [pid 1713]     return self._sslobj.write(data)
[Thu Feb 06 17:52:28.818793 2020] [:error] [pid 1713]   File "/usr/lib64/python3.6/ssl.py", line 642, in write
[Thu Feb 06 17:52:28.818795 2020] [:error] [pid 1713]     return self._sslobj.write(data)
[Thu Feb 06 17:52:28.818797 2020] [:error] [pid 1713] OSError: [Errno 14] Bad address
python django django-rest-framework socket.io amazon-elastic-beanstalk
1个回答
0
投票

坦白地说,这可能只是有时会发生的间歇性故障,尤其是在流量较高时。查看您在帖子中添加的例外,消息表明与套接字另一端的连接突然断开,可能是由于您的网络端跳过了与外部的节拍连接。

作为一种可能的解决方案,我会简单地捕获OSError并重试发送,可能会有少量退让,从而不会立即遇到相同的问题。

© www.soinside.com 2019 - 2024. All rights reserved.