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)
Это код, который отображает символ «Q». Проблема в том, что у меня есть только одна сопрограмма, которая отправляет данные в очередь для отображения, но время от времени я вижу дублирующийся вывод. Я могу предоставить дополнительную информацию.
Как решить эту проблему?
Большинство кадров:

Но это тоже появляется:

И даже этот:

Если я удалю display_queue, он все равно воспроизводит:
FRAME_INTERVAL = 0.01 # seconds
async def display(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:
now = time.monotonic()
to_wait = last + FRAME_INTERVAL - now
if to_wait > 0:
await asyncio.sleep(to_wait)
stdscr.clear()
stdscr.addstr(0, 0, 'Q')
stdscr.refresh()
last = time.monotonic()
except Exception as e:
pass
Подробнее здесь: https://stackoverflow.com/questions/798 ... rial-queue
Мобильная версия