2

I have a simple flask pp in python using gunicorn which I am running like: gunicorn -w 2 --bind 0.0.0.0:5000 --timeout 999 --log-level debug ers_intent_api:app . Is it possible to trigger a function to be run across all gunicorn workers?

for example say I have a simple app like:

app = Flask(__name__)
# initialize using some default file on import 
ir = IrObject.fromFile(file_)

@app.route('/retrain', methods=["POST"])
def retrain():
    """updates global ir object from passed file"""
    global ir
    # update global object programtically - this will only execute for a single worker
    file_ = request.files.get("file")
    ir = IrObject.fromFile(file_)

Basically, I want to update the global ir object for each gunicorn worker, but as it is now only one worker would be affected.

Is there a way that I could trigger the above function for all gunicorn workers?

Thanks in advance

Update: reposting this as someone closed my previous question assuming this is a duplicate of this question but this is not the case. The aforementioned question discusses persisting some object across requests to whereas I want to execute code across a set of gunicorn workers - very different things (unless I am misunderstanding the question).

Mason Edmison
  • 594
  • 3
  • 16
  • 1
    you can save it database or file and every worker will have to read this on every request. – furas Apr 23 '21 at 00:38

0 Answers0