Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse message first to ensure message sequence #15

Merged
merged 1 commit into from
Sep 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions gor/asyncio_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,47 @@ def __init__(self, *args, **kwargs):
self.q = asyncio.Queue()
self.concurrency = kwargs.get('concurrency', 2)
self.tasks = []
self.queues = []

async def _worker(self):
async def _worker(self, queue):
while True:
line = await self.q.get()
try:
msg = self.parse_message(line)
if msg:
self.emit(msg)
msg = await queue.get()
self.emit(msg)
finally:
self.q.task_done()
queue.task_done()

async def _stdin_reader(self):
while True:
try:
await asyncio.sleep(0)
line = sys.stdin.readline()
except KeyboardInterrupt:
await self.q.join()
await self._wait_task_queues()
self._stop()
break
if not line:
await self.q.join()
await self._wait_task_queues()
self._stop()
break
await self.q.put(line)
msg = self.parse_message(line)
h = hash(msg.id) % len(self.queues)
await self.queues[h].put(msg)

async def _run(self):
for _ in range(self.concurrency):
t = self.io_loop.create_task(self._worker())
q = asyncio.Queue()
self.queues.append(q)
t = self.io_loop.create_task(self._worker(q))
self.tasks.append(t)

stdin_reader_task = self.io_loop.create_task(self._stdin_reader())
self.tasks.append(stdin_reader_task)

async def _wait_task_queues(self):
for queue in self.queues:
await queue.join()

def _stop(self):
for t in self.tasks:
t.cancel()
Expand Down