I’m new to web development in Python and I’ve chosen Flask to start my web application. I have some resources to free before application shutdown, but I couldn’t find where to put my cleanup code.
Flask provides some decorators like before_request and teardown_request to register callbacks before and after request processing. Is there something similar to register a callback to be called before the application stops?
Thanks.
I’m new to web development in Python and I’ve chosen Flask to start my
Share
The
atexitmodule allows you to register program termination callbacks. Its callbacks won’t be called however if the application is terminated by a signal. If you need to handle those cases, you can register the same callbacks with thesignalmodule (for instance you might want to handle the SIGTERM signal).I may have misunderstood what exactly you want to cleanup, but resources such as file handles or database connections will be closed anyway at interpreter shutdown, so you shouldn’t have to worry about those.