문제

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

도움이 되었습니까?

해결책

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top