jupyter-server / jupyter_server

The backend—i.e. core services, APIs, and REST endpoints—to Jupyter web applications.
https://jupyter-server.readthedocs.io
BSD 3-Clause "New" or "Revised" License
484 stars 295 forks source link

Add async post_start method to ExtensionApp #1203

Open Zsailer opened 1 year ago

Zsailer commented 1 year ago

Problem

I'm working on server extension that needs to trigger an asyncio.Task as soon as the Tornado Web Server starts. (If I try to start it any sooner, we'll get the infamous RuntimeError: no running event loop). Currently, there is no easy way to do this using the provided Server Extension API.

Proposed Solution

I'd like to propose that we add a async def post_start(...) method/API to the ExtensionApp that can be used by authors to trigger tasks after the webserver (and presumably the main event loop) is started. This works nicely with the async def stop() API that's already in-place to cleanly stop async tasks in server extensions.

davidbrochart commented 1 year ago

FastAPI has startup and shutdown events, I don't know if it makes sense to have a similar API?

fcollonval commented 1 year ago

I ran exactly in the same issue :wink: that would be a great addition

oliver-sanders commented 5 months ago

This has also come up in https://github.com/jupyter-server/jupyter_server/issues/1329