From b6df63d8492c9a18e7d4a7ea5e8378e076b7a723 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Sat, 14 May 2016 21:39:23 -0700 Subject: [PATCH] delay start of message queue listener thread until first request comes --- socketio/server.py | 5 ++++- tests/test_server.py | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/socketio/server.py b/socketio/server.py index 32b4e79..7a5ab24 100644 --- a/socketio/server.py +++ b/socketio/server.py @@ -96,8 +96,8 @@ class Server(object): if client_manager is None: client_manager = base_manager.BaseManager() - client_manager.initialize(self) self.manager = client_manager + self.manager_initialized = False def on(self, event, handler=None, namespace=None): """Register an event handler. @@ -300,6 +300,9 @@ class Server(object): This function returns the HTTP response body to deliver to the client as a byte sequence. """ + if not self.manager_initialized: + self.manager_initialized = True + self.manager.initialize(self) return self.eio.handle_request(environ, start_response) def start_background_task(self, target, *args, **kwargs): diff --git a/tests/test_server.py b/tests/test_server.py index 470e27a..b0ae993 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -21,10 +21,13 @@ class TestServer(unittest.TestCase): def test_create(self, eio): mgr = mock.MagicMock() s = server.Server(client_manager=mgr, binary=True, foo='bar') + s.handle_request({}, None) + s.handle_request({}, None) eio.assert_called_once_with(**{'foo': 'bar'}) self.assertEqual(s.manager, mgr) self.assertEqual(s.eio.on.call_count, 3) self.assertEqual(s.binary, True) + self.assertEqual(mgr.initialize.call_count, 1) def test_on_event(self, eio): s = server.Server() @@ -285,6 +288,7 @@ class TestServer(unittest.TestCase): def test_handle_event_binary_ack(self, eio): s = server.Server() + s.manager.initialize(s) s._handle_eio_message('123', '61-1["my message","a",' '{"_placeholder":true,"num":0}]') self.assertEqual(s._attachment_count, 1)