Working with a global singleton in Flask (WSGI), do I have to worry about race conditions? Working with a global singleton in Flask (WSGI), do I have to worry about race conditions? python python

Working with a global singleton in Flask (WSGI), do I have to worry about race conditions?


You could try the Local class from werkzeug.Here's some info about it: Context Locals

Example:

from flask import Flaskfrom werkzeug.local import Localapp = Flask(__name__)loc = Local()loc.a = 1loc.b = 2loc.c = 3@app.route("/")def hello():    loc.a += 1    loc.b += loc.a    loc.c += loc.b    return "Hello World!"if __name__ == "__main__":    app.run()


You might take a look at the g object that you can import directly from flask, keeps an object globally for that request. If you're using an event driven WSGI server (tornado, gevent, etc) you shouldn't have any issues.