I have a Flask app running behind Apache HTTPD. Apache is configured to have multiple child processes.
The Flask app creates a file on the server with the file's name equal to its process ID. The code looks something like this:
import os
@app.before_first_request
def before_first_request():
filename = os.getpid()
with open(filename, 'w') as file:
file.write('Hello')
When the child process is killed/ended/terminated I would like the Flask app to remove this file.
It's not terribly important that removal of a file happens, as these files won't take up much space, so if bizarre errors occur I don't need to handle them. But for normal workflow, I would like to have some cleanup when Apache shuts down the Flask process.
Any idea on the best way to do this?