From ec7b3da2e48060e177bf8ad6a9f5fae445207c82 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Sat, 30 Apr 2022 16:06:27 +0100 Subject: [PATCH] Better handling of queued WebSocket messages in uWSGI (Fixes #256) --- src/engineio/async_drivers/gevent_uwsgi.py | 24 +++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/engineio/async_drivers/gevent_uwsgi.py b/src/engineio/async_drivers/gevent_uwsgi.py index 43cf698..d35fc6a 100644 --- a/src/engineio/async_drivers/gevent_uwsgi.py +++ b/src/engineio/async_drivers/gevent_uwsgi.py @@ -26,6 +26,7 @@ class uWSGIWebSocket(object): # pragma: no cover def __init__(self, app): self.app = app self._sock = None + self.received_messages = [] def __call__(self, environ, start_response): self._sock = uwsgi.connection_fd() @@ -117,6 +118,9 @@ def wait(self): return None return self._decode_received(msg) else: + if self.received_messages: + return self.received_messages.pop(0) + # we wake up at least every 3 seconds to let uWSGI # do its ping/ponging event_set = self._event.wait(timeout=3) @@ -133,13 +137,19 @@ def wait(self): self._send(msg) # maybe there is something to receive, if not, at least # ensure uWSGI does its ping/ponging - try: - msg = uwsgi.websocket_recv_nb() - except IOError: # connection closed - self._select_greenlet.kill() - return None - if msg: # message available - return self._decode_received(msg) + while True: + try: + msg = uwsgi.websocket_recv_nb() + except IOError: # connection closed + self._select_greenlet.kill() + return None + if msg: # message available + self.received_messages.append( + self._decode_received(msg)) + else: + break + if self.received_messages: + return self.received_messages.pop(0) _async = {