Startup / shutdown events in Django

Hi folks,
I want to continue the discussion started here. I’m also concerned by the shutdown stuff, in my use case, it is to close a scheduler (APscheduler). I know celery but I don’t want to use it.

I want to know if there is a way to run functions at the shutdown of the application as we can do with some ASGI web frameworks like FastAPI.

No, because you have no control over it. There are any number of circumstances where you’re not going to received any notification that you’re being shutdown. (Think power outage.)

Then go ahead and use it. There’s no reason not to.

1 Like

Gunicorn has an on exit hook. Since your application should be run by a wsgi server in production (like gunicorn, uwsgi, waitress, etc), not with runserver, you should look at your wsgi server docs for more find server related hooks/configuration.

I also want to echo that @KenWhitesell’s points, in this post and the linked one, are all very valid.

1 Like

Thanks for the replies