@@ -526,9 +526,9 @@ class Websocket:
526526 def __init__ (
527527 self ,
528528 ws_url : str ,
529- max_subscriptions = 1024 ,
530- max_connections = 100 ,
531- shutdown_timer = 5 ,
529+ max_subscriptions : int = 1024 ,
530+ max_connections : int = 100 ,
531+ shutdown_timer : Optional [ float ] = 5.0 ,
532532 options : Optional [dict ] = None ,
533533 _log_raw_websockets : bool = False ,
534534 retry_timeout : float = 60.0 ,
@@ -542,7 +542,9 @@ def __init__(
542542 ws_url: Websocket URL to connect to
543543 max_subscriptions: Maximum number of subscriptions per websocket connection
544544 max_connections: Maximum number of connections total
545- shutdown_timer: Number of seconds to shut down websocket connection after last use
545+ shutdown_timer: Number of seconds to shut down websocket connection after last use. If set to `None`, the
546+ connection will never be automatically shut down. Use this for very long-running processes, where you
547+ will manually shut down the connection if ever you intend to close it.
546548 options: Options to pass to the websocket connection
547549 _log_raw_websockets: Whether to log raw websockets in the "raw_websocket" logger
548550 retry_timeout: Timeout in seconds to retry websocket connection
@@ -661,23 +663,25 @@ async def _handler(self, ws: ClientConnection) -> Union[None, Exception]:
661663 return e
662664
663665 async def __aexit__ (self , exc_type , exc_val , exc_tb ):
664- if not self .state != State .CONNECTING :
665- if self ._exit_task is not None :
666- self ._exit_task .cancel ()
667- try :
668- await self ._exit_task
669- except asyncio .CancelledError :
670- pass
671- if self .ws is not None :
672- self ._exit_task = asyncio .create_task (self ._exit_with_timer ())
666+ if self .shutdown_timer is not None :
667+ if not self .state != State .CONNECTING :
668+ if self ._exit_task is not None :
669+ self ._exit_task .cancel ()
670+ try :
671+ await self ._exit_task
672+ except asyncio .CancelledError :
673+ pass
674+ if self .ws is not None :
675+ self ._exit_task = asyncio .create_task (self ._exit_with_timer ())
673676
674677 async def _exit_with_timer (self ):
675678 """
676679 Allows for graceful shutdown of websocket connection after specified number of seconds, allowing
677680 for reuse of the websocket connection.
678681 """
679682 try :
680- await asyncio .sleep (self .shutdown_timer )
683+ if self .shutdown_timer is not None :
684+ await asyncio .sleep (self .shutdown_timer )
681685 await self .shutdown ()
682686 except asyncio .CancelledError :
683687 pass
0 commit comments