Replace semaphore with queue

This commit is contained in:
Andrew Ferrazzutti 2021-04-21 18:09:21 -04:00
parent 0ce6b833e3
commit 99aa333bc2
1 changed files with 4 additions and 6 deletions

View File

@ -50,8 +50,7 @@ class RPCClient:
self._response_waiters = {} self._response_waiters = {}
self._writer = None self._writer = None
self._reader = None self._reader = None
self._command_queue: List[Tuple[int, str, Dict[str, Any]]] = [] self._command_queue = asyncio.Queue()
self._command_sem = asyncio.Semaphore(0)
async def connect(self) -> None: async def connect(self) -> None:
if self._writer is not None: if self._writer is not None:
@ -120,8 +119,7 @@ class RPCClient:
if not is_sequential: if not is_sequential:
self.loop.create_task(self._run_event_handler(req_id, command, req)) self.loop.create_task(self._run_event_handler(req_id, command, req))
else: else:
self._command_queue.append((req_id, command, req)) self._command_queue.put_nowait((req_id, command, req))
self._command_sem.release()
return return
try: try:
waiter = self._response_waiters[req_id] waiter = self._response_waiters[req_id]
@ -137,9 +135,9 @@ class RPCClient:
async def _command_loop(self) -> None: async def _command_loop(self) -> None:
while True: while True:
await self._command_sem.acquire() req_id, command, req = await self._command_queue.get()
req_id, command, req = self._command_queue.pop(0)
await self._run_event_handler(req_id, command, req) await self._run_event_handler(req_id, command, req)
self._command_queue.task_done()
async def _try_read_loop(self) -> None: async def _try_read_loop(self) -> None:
try: try: