stdscr overlaps and mix output in a serial queue

15 hours ago 3
ARTICLE AD BOX
FRAME_INTERVAL = 0.01 # seconds display_queue = asyncio.Queue() async def display(display_queue, stdscr): curses.curs_set(0) curses.start_color() curses.use_default_colors() curses.init_pair(1, curses.COLOR_RED, -1) curses.init_pair(2, curses.COLOR_GREEN, -1) last = time.monotonic() while True: try: item = await display_queue.get() while not display_queue.empty(): item = await display_queue.get() now = time.monotonic() to_wait = last + FRAME_INTERVAL - now if to_wait > 0: await asyncio.sleep(to_wait) if item is not None: stdscr.clear() stdscr.addstr(0, 0, 'Q') stdscr.refresh() last = time.monotonic() except Exception as e: pass # initialized in async main: display_coro = display(display_queue, stdscr) await asyncio.gather(worker_coro, display_coro)

This is a code which displays a 'Q' character. The problem is that I have only 1 coroutine which sends data to the queue to display but from time to time I can see duplicated output. I can provide more information.
How to resolve it?

Most of frames:

enter image description here

But this appears too:

enter image description here

And even this one:

enter image description here

Read Entire Article