datasette icon indicating copy to clipboard operation
datasette copied to clipboard

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

Open simonw opened this issue 5 months ago • 1 comments

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:

https://github.com/simonw/datasette/blob/7a602140df3646820adc45963daf7fc5dcd2a009/datasette/app.py#L591-L592

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

simonw avatar Aug 14 '25 18:08 simonw

I wonder if this works for all of the other hooks using that pattern as well?

Need comprehensive tests to prove it before I document this.

simonw avatar Aug 14 '25 18:08 simonw