简体   繁体   中英

Flask + uwsgi + threading

I have a flask application which I want to perform an update task every minute via a thread.

The thread is setup like this:

def print_thread():
    print "I am a thread"

@app.before_first_request
def start_thread():
    threading.Timer(60, print_thread).start()

The flask application is running via uwsgi :

uwsgi_python -s /tmp/uwsgi.sock --processes 1 --threads 4 -w app:app --enable-threads

I have run into this problem before and solved it by having a flask endpoint which is called every minute via cron , but I want a cleaner solution which is self contained in the flask application.

Can anybody identify the problem?

Or know of a clean solution to solve this problem?

Thanks

I suggest avoiding running background tasks in the same process as your flask instances created through WSGI. That allows you to be sure that with multiple processes\\threads created through uwsgi, background tasks won't get duplicated.

You can launch a separate python process from different file and use a python schedule, for example, apscheduler .

from apscheduler.schedulers.background import BlockingScheduler

from app import create_app # Your app factory
from app import job # A job function

# Most probably, your background job will depend on your app being initialized
# If you don't use the app factory pattern, you can simply import a file containing your app to trigger initialization. 
app = create_app() 

scheduler = BlockingScheduler()
scheduler.add_job(job, 'interval', minutes=1)

scheduler.start()

Then you can place this script alongside your app.py and launch a process with python your_name.py and keep it running together with uwsgi. This way your web app and background tasks will share code and configurations, but the processes will be clearly separated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM