Skip to content
Advertisement

Atomic Code in gunicorn multiprocessing / only run code in worker 1?

I am new to gunicorn multiprocessing (by calling gunicorn --worker=X). I am using it with Flask to provide the WSGI implementation for our productive frontend. To use multiprocessing, we pass the above mentioned parameter to unicorn.

Our Flask application also uses APScheduler (via Flask-APScheduler) to run a cron task every Y hours. This task searches for new database entries to process, and when it finds them, starts processing them one by one. The process should only be run by one worker obviously. But because of gunicorn, X workers are now spawned, each running the task every X hours, creating race conditions.

Is there a way to make the code atomic so that I can set the “processed” variable in the DB entry to true? Or, maybe, tell gunicorn to only run that specific code on the parent process, or first spawned worker?

Thanks for every input! :-)

Advertisement

Answer

The --preload parameter for gunicorn gives an opportunity to run code just in the parent worker.

All the code that is run before app.run() (or whatever you called your Flask() object) is apparently run on the parent process. Didn’t find any documentation on this unfortunately, but this post lead me to it.

So, running the APScheduler code before it makes sure that it’s only run (or registered, in this case) once.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement