Working With A Global Singleton In Flask (WSGI), Do I Have To Worry About Race Conditions?
The hello world demo for Flask is: from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return 'Hello World!' if __name__ == '__main__': app.run()
Solution 1:
You could try the Local class from werkzeug. Here's some info about it: Context Locals
Example:
from flask import Flask
from werkzeug.local import Local
app = Flask(__name__)
loc = Local()
loc.a = 1
loc.b = 2
loc.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()
Solution 2:
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.
Post a Comment for "Working With A Global Singleton In Flask (WSGI), Do I Have To Worry About Race Conditions?"