Add locking for multiple reciever

main
Adib Pratama 1 day ago
parent 6a4dabf28f
commit 7175cc5ef3
No known key found for this signature in database
GPG Key ID: 7C855EE276A46D2C
  1. 2
      src/nanobot.py

@ -14,6 +14,7 @@ class Nanobot:
def __init__(self): def __init__(self):
self._ws = None self._ws = None
self._chat_id = None self._chat_id = None
self._lock = asyncio.Lock()
async def __aenter__(self): async def __aenter__(self):
await self.connect() await self.connect()
@ -30,6 +31,7 @@ class Nanobot:
main_logger.info("connected chat_id=%s", self._chat_id) main_logger.info("connected chat_id=%s", self._chat_id)
async def chat(self, message: str, timeout: float = 60): async def chat(self, message: str, timeout: float = 60):
async with self._lock:
try: try:
await self._ws.send(json.dumps({"content": message})) await self._ws.send(json.dumps({"content": message}))
except websockets.ConnectionClosed: except websockets.ConnectionClosed:

Loading…
Cancel
Save