Skip to content

Document that async def startup() hook functions actually do work #2497

@simonw

Description

@simonw

Discovered this by accident today: the following works:

@hookimpl
async def startup(datasette):
    await asyncio.sleep(0.1)
    print("Startup hook fired")

I had previously thought you needed to do this instead:

@hookimpl
def startup(datasette):
    async def inner():
        await asyncio.sleep(0.1)
        print("Startup hook fired")
    return inner

It turns out Pluggy (at least up to 1.6.0 the current version) doesn't feature the keywords asyncio or await anywhere in the codebase... but the above works anyway because Pluggy ends up returning a coroutine that needs to be awaited and Datasette then does this:

datasette/datasette/app.py

Lines 591 to 592 in 7a60214

for hook in pm.hook.startup(datasette=self):
await await_me_maybe(hook)

And await_me_maybe() does the right thing with that coroutine that Pluggy returned!

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions