Python 尝试在Django Rest框架视图中发出socketio消息有时会失败

Python 尝试在Django Rest框架视图中发出socketio消息有时会失败,python,django,django-rest-framework,socket.io,amazon-elastic-beanstalk,Python,Django,Django Rest Framework,Socket.io,Amazon Elastic Beanstalk,在某些情况下,我的Django Rest Framework应用程序需要使用socketio向另一台服务器发送通知。用于发出通知的函数包含在views.py文件中,并且在需要时直接从Rest请求处理程序调用该函数 大多数情况下,它工作正常,但有时调用emit()会引发异常。到目前为止,至少发生了三种不同的例外情况 该应用程序在Elastic Beanstalk上运行 用于发出消息的代码: def send_request(message): sio = socketio.Client()

在某些情况下,我的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

老实说,这可能只是偶尔发生的间歇性故障,尤其是在较高流量下。查看您在帖子中添加的异常,这些消息表明与套接字另一端的连接突然断开,可能是由于您的网络端跳过了与外部的beat连接

作为一种可能的解决方案,我只需捕获
OSError
,然后重试发送,可能会有一个小回退,以避免立即遇到相同的问题