Skip to content

Commit 11461ff

Browse files
committed
Ensure everything uses loop time rather than time.time
1 parent f9c6228 commit 11461ff

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import logging
1010
import ssl
1111
import time
12+
import warnings
1213
from hashlib import blake2b
1314
from typing import (
1415
Optional,
@@ -530,23 +531,31 @@ def __init__(
530531
self._exit_task = None
531532
self._open_subscriptions = 0
532533
self._options = options if options else {}
533-
self.last_received = time.time()
534-
self.last_sent = time.time()
534+
try:
535+
now = asyncio.get_running_loop().time()
536+
except RuntimeError:
537+
warnings.warn(
538+
"You are instantiating the AsyncSubstrateInterface Websocket outside of an event loop. "
539+
"Verify this is intended."
540+
)
541+
now = asyncio.new_event_loop().time()
542+
self.last_received = now
543+
self.last_sent = now
535544

536545
async def __aenter__(self):
537546
async with self._lock:
538547
self._in_use += 1
539548
await self.connect()
540-
now = asyncio.get_running_loop().time()
541-
self.last_received = now
542-
self.last_sent = now
543549
return self
544550

545551
@staticmethod
546552
async def loop_time() -> float:
547553
return asyncio.get_running_loop().time()
548554

549555
async def connect(self, force=False):
556+
now = await self.loop_time()
557+
self.last_received = now
558+
self.last_sent = now
550559
if self._exit_task:
551560
self._exit_task.cancel()
552561
if not self._initialized or force:

0 commit comments

Comments
 (0)