Skip to content

Commit 5452877

Browse files
authored
Merge pull request #197 from opentensor/feat/thewhaleking/allow-no-shutdown-timer
Allows AsyncSubstrateInterface's Websocket connection to not automatically shut down
2 parents e1b1772 + d47af88 commit 5452877

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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
@@ -659,25 +661,28 @@ async def _handler(self, ws: ClientConnection) -> Union[None, Exception]:
659661
return e
660662
elif isinstance(e := send_task.result(), Exception):
661663
return e
664+
return None
662665

663666
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())
667+
if self.shutdown_timer is not None:
668+
if not self.state != State.CONNECTING:
669+
if self._exit_task is not None:
670+
self._exit_task.cancel()
671+
try:
672+
await self._exit_task
673+
except asyncio.CancelledError:
674+
pass
675+
if self.ws is not None:
676+
self._exit_task = asyncio.create_task(self._exit_with_timer())
673677

674678
async def _exit_with_timer(self):
675679
"""
676680
Allows for graceful shutdown of websocket connection after specified number of seconds, allowing
677681
for reuse of the websocket connection.
678682
"""
679683
try:
680-
await asyncio.sleep(self.shutdown_timer)
684+
if self.shutdown_timer is not None:
685+
await asyncio.sleep(self.shutdown_timer)
681686
await self.shutdown()
682687
except asyncio.CancelledError:
683688
pass

0 commit comments

Comments
 (0)