-
Notifications
You must be signed in to change notification settings - Fork 31
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
Transition to asyncio #315
Comments
from discord: I am having a look at the asyncio transition the main issue I am facing is that;
if some action get slow so, I introduced a queue scrapping mechanism in the second loop before the actin is taken to removed the in between drag/move positions and process only the last one in the queue. this is kind of easy using threads because you know that the first queue is filled also if the second thread got stuck with some heavyload operation. but I am kind of struggling to reproduce this behavior using asyncio. import asyncio
import sys
import time
async def read_stdin(queue):
"""Coroutine to read lines from stdin and push them to the queue."""
loop = asyncio.get_event_loop()
reader = asyncio.StreamReader()
protocol = asyncio.StreamReaderProtocol(reader)
await loop.connect_read_pipe(lambda: protocol, sys.stdin)
while True:
# Read all available lines from stdin
line = await reader.readline()
print(f"Read: {line}")
if line:
queue.put_nowait(line.decode().strip())
while not reader.at_eof():
line = await reader.readline()
print(f"Read: {line}")
if line:
queue.put_nowait(line.decode().strip())
async def process_queue(queue):
"""Coroutine to read lines from the queue and print them."""
while True:
lines = []
# Collect all lines from the queue
line = await queue.get()
lines.append(line)
queue.task_done()
while not queue.empty():
line = await queue.get()
if line is None:
return # Stop signal
lines.append(line)
queue.task_done()
# Print all collected lines
if lines:
print("Processed batch:")
for line in lines:
print(f" - Processed: {line}")
await asyncio.sleep(0.01)
print('Sleep ...')
time.sleep(2)
print('Sleep Done')
async def main():
queue = asyncio.Queue()
# Start the read_stdin and process_queue coroutines
read_task = asyncio.create_task(read_stdin(queue))
process_task = asyncio.create_task(process_queue(queue))
# Wait for both tasks to complete
await asyncio.gather(read_task, process_task)
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
pass so far the first coroutine is able to collect all the input sent during the sleep |
No description provided.
The text was updated successfully, but these errors were encountered: