textual icon indicating copy to clipboard operation
textual copied to clipboard

Docs about async event handlers recommand the asyncio API instead of Workers

Open pthebaul opened this issue 5 months ago • 2 comments

Current text and code:

Network access is a common cause of slow handlers. If you try to retrieve a file from the internet, the message handler may take anything up to a few seconds to return, which would prevent the widget or app from updating during that time. The solution is to launch a new asyncio task to do the network task in the background.

async def on_input_changed(self, message: Input.Changed) -> None:
    """A coroutine to handle a text changed message."""
    if message.value:
        # Look up the word in the background
        asyncio.create_task(self.lookup_word(message.value))
    else:
        # Clear the results
        self.query_one("#results", Static).update()

As Will said on Discord:

It is still true, but if would definitely be a good place to induce workers!

pthebaul avatar Jun 27 '25 12:06 pthebaul