Skip to content

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

Open
@pthebaul

Description

@pthebaul

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions