Skip to content

Commit 1ad38c5

Browse files
committed
Handle different loops
1 parent bff6a1a commit 1ad38c5

File tree

1 file changed

+5
-21
lines changed

1 file changed

+5
-21
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -546,28 +546,14 @@ def __init__(
546546
self.retry_timeout = retry_timeout
547547
self._received: dict[str, asyncio.Future] = {}
548548
self._received_subscriptions: dict[str, asyncio.Queue] = {}
549-
self._sending = asyncio.Queue()
549+
self._sending: Optional[asyncio.Queue] = None
550550
self._send_recv_task = None
551551
self._inflight: dict[str, str] = {}
552552
self._attempts = 0
553553
self._lock = asyncio.Lock()
554554
self._exit_task = None
555555
self._options = options if options else {}
556556
self._log_raw_websockets = _log_raw_websockets
557-
558-
try:
559-
now = asyncio.get_running_loop().time()
560-
except RuntimeError:
561-
warnings.warn(
562-
"You are instantiating the AsyncSubstrateInterface Websocket outside of an event loop. "
563-
"Verify this is intended."
564-
)
565-
# default value for in case there's no running asyncio loop
566-
# this really doesn't matter in most cases, as it's only used for comparison on the first call to
567-
# see how long it's been since the last call
568-
now = 0.0
569-
self.last_received = now
570-
self.last_sent = now
571557
self._in_use_ids = set()
572558

573559
@property
@@ -603,10 +589,9 @@ async def _cancel(self):
603589
)
604590

605591
async def connect(self, force=False):
606-
now = await self.loop_time()
607-
self.last_received = now
608-
self.last_sent = now
609592
async with self._lock:
593+
if self._sending is None or self._sending.empty():
594+
self._sending = asyncio.Queue()
610595
if self._exit_task:
611596
self._exit_task.cancel()
612597
if self.state not in (State.OPEN, State.CONNECTING) or force:
@@ -683,7 +668,6 @@ async def _recv(self, recd: bytes) -> None:
683668
if self._log_raw_websockets:
684669
raw_websocket_logger.debug(f"WEBSOCKET_RECEIVE> {recd.decode()}")
685670
response = json.loads(recd)
686-
self.last_received = await self.loop_time()
687671
if "id" in response:
688672
async with self._lock:
689673
self._inflight.pop(response["id"])
@@ -707,7 +691,7 @@ async def _start_receiving(self, ws: ClientConnection) -> Exception:
707691
)
708692
await self._recv(recd)
709693
except Exception as e:
710-
logger.exception("Start receving exception", exc_info=e)
694+
logger.exception("Start receiving exception", exc_info=e)
711695
if isinstance(e, ssl.SSLError):
712696
e = ConnectionClosed
713697
for fut in self._received.values():
@@ -728,8 +712,8 @@ async def _start_sending(self, ws) -> Exception:
728712
if self._log_raw_websockets:
729713
raw_websocket_logger.debug(f"WEBSOCKET_SEND> {to_send}")
730714
await ws.send(to_send)
731-
self.last_sent = await self.loop_time()
732715
except Exception as e:
716+
logger.exception("Start sending exception", exc_info=e)
733717
if to_send is not None:
734718
self._received[to_send["id"]].set_exception(e)
735719
self._received[to_send["id"]].cancel()

0 commit comments

Comments
 (0)