prompt-toolkit / python-prompt-toolkit

Library for building powerful interactive command line applications in Python
https://python-prompt-toolkit.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
9.11k stars 718 forks source link

Why does my prompt-toolkit Dialog flicker on terminal output with asyncio and patch_stout? #1698

Open harrandt opened 1 year ago

harrandt commented 1 year ago

When running a prompt-toolkit Dialog it will flicker if there is terminal output from a different asyncio task.

I am using the context with patch_stdout() within a task as mentioned in the doc, at least as far as I understand it. I read somewhere that starting with prompt-toolkit 3.0 it uses the default asyncio event loop and does not create one itself.

And since asyncio.run always creates a new event loop and closes it at the end and the context manager is within that, I have no clue what could be the reason for the flickering. What am I missing?

(Python 3.9, prompt-toolkit 3.0.36)

This is a MCVE:

import asyncio
from prompt_toolkit.patch_stdout import patch_stdout
from prompt_toolkit.shortcuts.dialogs import _create_app, _return_none
from prompt_toolkit.widgets import Button, Dialog, Label

dialog_align = Dialog(
    title='Please align',
    body=Label(text="init", dont_extend_height=True),
    buttons=[Button(text='Start measurement', width=21, handler=_return_none)],
    with_background=True,
)

async def prompt_align():

    return await _create_app(dialog_align, style=None).run_async()

async def main_datasource():

    while True:
        await asyncio.sleep(0.5)
        print("test")

async def main():

    with patch_stdout():

        task1 = asyncio.create_task(prompt_align())
        task2 = asyncio.create_task(main_datasource())

        await asyncio.gather(task1, task2)

if __name__ == "__main__":

    try:
        from asyncio import run
    except ImportError:
        asyncio.run_until_complete(main())
    else:

        asyncio.run(main())