Skip to content

Commit c419fc5

Browse files
Add a shutdown() function for the server
1 parent 699ee9c commit c419fc5

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

src/socketio/asyncio_server.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,15 @@ async def disconnect(self, sid, namespace=None, ignore_queue=False):
387387
await self.manager.disconnect(sid, namespace=namespace,
388388
ignore_queue=True)
389389

390+
async def shutdown(self):
391+
"""Stop Socket.IO background tasks.
392+
393+
This method stops all background activity initiated by the Socket.IO
394+
server. It must be called before shutting down the web server.
395+
"""
396+
self.logger.info('Socket.IO is shutting down')
397+
await self.eio.shutdown()
398+
390399
async def handle_request(self, *args, **kwargs):
391400
"""Handle an HTTP request from the client.
392401

src/socketio/server.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,15 @@ def disconnect(self, sid, namespace=None, ignore_queue=False):
570570
self.manager.disconnect(sid, namespace=namespace,
571571
ignore_queue=True)
572572

573+
def shutdown(self):
574+
"""Stop Socket.IO background tasks.
575+
576+
This method stops all background activity initiated by the Socket.IO
577+
server. It must be called before shutting down the web server.
578+
"""
579+
self.logger.info('Socket.IO is shutting down')
580+
self.eio.shutdown()
581+
573582
def transport(self, sid):
574583
"""Return the name of the transport used by the client.
575584

tests/asyncio/test_asyncio_server.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,12 @@ def test_async_handlers(self, eio):
981981
None,
982982
)
983983

984+
def test_shutdown(self, eio):
985+
s = asyncio_server.AsyncServer()
986+
s.eio.shutdown = AsyncMock()
987+
_run(s.shutdown())
988+
s.eio.shutdown.mock.assert_called_once_with()
989+
984990
def test_start_background_task(self, eio):
985991
s = asyncio_server.AsyncServer()
986992
s.start_background_task('foo', 'bar', baz='baz')

tests/common/test_server.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,11 @@ def test_async_handlers(self, eio):
917917
None,
918918
)
919919

920+
def test_shutdown(self, eio):
921+
s = server.Server()
922+
s.shutdown()
923+
s.eio.shutdown.assert_called_once_with()
924+
920925
def test_start_background_task(self, eio):
921926
s = server.Server()
922927
s.start_background_task('foo', 'bar', baz='baz')

0 commit comments

Comments
 (0)