@@ -24,6 +24,9 @@ class AsyncServer(server.Server):
2424 packets. Custom json modules must have ``dumps`` and ``loads``
2525 functions that are compatible with the standard library
2626 versions.
27+ :param async_handlers: If set to ``True``, event handlers are executed in
28+ separate threads. To run handlers synchronously,
29+ set to ``False``. The default is ``False``.
2730 :param kwargs: Connection parameters for the underlying Engine.IO server.
2831
2932 The Engine.IO configuration supports the following settings:
@@ -55,11 +58,13 @@ class AsyncServer(server.Server):
5558 a logger object to use. To disable logging set to
5659 ``False``.
5760 """
58- def __init__ (self , client_manager = None , logger = False , json = None , ** kwargs ):
61+ def __init__ (self , client_manager = None , logger = False , json = None ,
62+ async_handlers = False , ** kwargs ):
5963 if client_manager is None :
6064 client_manager = asyncio_manager .AsyncManager ()
6165 super ().__init__ (client_manager = client_manager , logger = logger ,
62- binary = False , json = json , ** kwargs )
66+ binary = False , json = json ,
67+ async_handlers = async_handlers , ** kwargs )
6368
6469 def is_asyncio_based (self ):
6570 return True
@@ -280,7 +285,11 @@ async def _handle_event(self, sid, namespace, id, data):
280285 namespace = namespace or '/'
281286 self .logger .info ('received event "%s" from %s [%s]' , data [0 ], sid ,
282287 namespace )
283- await self ._handle_event_internal (self , sid , data , namespace , id )
288+ if self .async_handlers :
289+ self .start_background_task (self ._handle_event_internal , self , sid ,
290+ data , namespace , id )
291+ else :
292+ await self ._handle_event_internal (self , sid , data , namespace , id )
284293
285294 async def _handle_event_internal (self , server , sid , data , namespace , id ):
286295 r = await server ._trigger_event (data [0 ], namespace , sid , * data [1 :])
0 commit comments