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

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

Open
harrandt opened this issue Dec 11, 2022 · 0 comments

Comments

@harrandt
Copy link

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())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant