3

When I put gunicorn in front of a Flask service that has a method like:

@app.route('/testr/', methods=['POST'])
def test():
  atomic_section_here

To my understanding, I can create multiple workers (lets say 5). Does this mean that I get 5 processes each with their own interpreter? Or do I get 5 processes spawned by one interpreter using multiprocessing?

If I want the critical section to be atomic, can I just initialize a lock at the top-level of this file and acquire it within the critical section?

Andy
  • 942
  • 1
  • 10
  • 23

1 Answers1

2

You get 5 processes spawned via multiprocessing from 1 interpreter. You can share a lock between workers with mutltiprocessing.Lock().aquire in your route and preload_app = True in gunicorn.conf.

Here is some reading about the design of Gunicorn, including the workers.

Here is a relevant question and answer about sharing the lock.

Community
  • 1
  • 1
Carson Crane
  • 1,197
  • 8
  • 15