Question

Is there any way to execute some code just before a worker is turned off? I'm not too confident on execution model of flask\werkzeug, the situation is this:

During the creation of flask application i start a deamon thread to do some external stuff (waiting on a queue essentially); i've setup this thread as demon because i don't want it to prevent the shut down of the worker running the flask application when it's needed. there is my problem: i need to execute some clean up code just before the thread it's been killed by the worker, and my solution is to do those operations on a termination event (if any) of the worker

Was it helpful?

Solution

With python you can use the uwsgi.atexit hook. The function callback will be executed before exit.

import uwsgi, os
from flask import Flask
app = Flask('demo')

@app.route('/')
def index():
    return "Hello World"

def callback():
    print "Worker %i exinting" % os.getpid()
uwsgi.atexit = callback
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top