2015-09-28 03:52:36 8 Comments
In my app the state of a common object is changed by making requests, and the response depends on the state.
class SomeObj():
def __init__(self, param):
self.param = param
def query(self):
self.param += 1
return self.param
global_obj = SomeObj(0)
@app.route('/')
def home():
flash(global_obj.query())
render_template('index.html')
If I run this on my development server, I expect to get 1, 2, 3 and so on. If requests are made from 100 different clients simultaneously, can something go wrong? The expected result would be that the 100 different clients each see a unique number from 1 to 100. Or will something like this happen:
- Client 1 queries.
self.param
is incremented by 1. - Before the return statement can be executed, the thread switches over to client 2.
self.param
is incremented again. - The thread switches back to client 1, and the client is returned the number 2, say.
- Now the thread moves to client 2 and returns him/her the number 3.
Since there were only two clients, the expected results were 1 and 2, not 2 and 3. A number was skipped.
Will this actually happen as I scale up my application? What alternatives to a global variable should I look at?
Related Questions
Sponsored Content
16 Answered Questions
7 Answered Questions
[SOLVED] Why are local variables thread safe in Java
- 2012-10-10 18:22:05
- Anand
- 42488 View
- 86 Score
- 7 Answer
- Tags: java thread-safety
1 Answered Questions
[SOLVED] Is reading a global collections.deque from within a Flask request safe?
- 2016-06-14 14:17:11
- Dirk
- 130 View
- 1 Score
- 1 Answer
- Tags: python flask thread-safety deque apscheduler
2 Answered Questions
6 Answered Questions
1 Answered Questions
[SOLVED] Flask-SocketIo, How to share data between python thread and socketio.start_background_task thread?
- 2018-11-19 16:48:32
- ctheadn8954
- 444 View
- 1 Score
- 1 Answer
- Tags: python multithreading flask flask-socketio
2 Answered Questions
[SOLVED] Flask POSTs with Trailing Slash
- 2017-03-08 01:15:54
- Matthew Moisen
- 1566 View
- 6 Score
- 2 Answer
- Tags: python python-2.7 flask
2 Answered Questions
[SOLVED] Is local static variable initialization thread-safe in C++11?
- 2011-11-12 02:34:55
- Ralph Zhang
- 57524 View
- 198 Score
- 2 Answer
- Tags: c++ thread-safety c++11
0 Answered Questions
How to have server call another server in Python with threading?
- 2015-06-20 05:04:24
- user1835351
- 71 View
- 0 Score
- 0 Answer
- Tags: python flask python-multithreading
2 comments
@lhk 2018-11-05 10:21:02
This is not really an answer to thread safety of globals.
But I think it is important to mention sessions here. You are looking for a way to store client-specific data. Every connection should have access to its own pool of data, in a threadsafe way.
This is possible with server-side sessions, and they are available in a very neat flask plugin: https://pythonhosted.org/Flask-Session/
If you set up sessions, a
session
variable is available in all your routes and it behaves like a dictionary. The data stored in this dictionary is individual for each connecting client.Here is a short demo:
After
pip install Flask-Session
, you should be able to run this. Try accessing it from different browsers, you'll see that the counter is not shared between them.@davidism 2015-09-28 14:26:28
You can't use global variables to hold this sort of data. Not only is it not thread safe, it's not process safe, and WSGI servers in production spawn multiple processes. Not only would your counts be wrong if you were using threads to handle requests, they would also vary depending on which process handled the request.
Use a data source outside of Flask to hold global data. A database, memcached, or redis are all appropriate separate storage areas, depending on your needs. If you need to load and access Python data, consider
multiprocessing.Manager
. You could also use the session for simple data that is per-user.The development server may run in single thread and process. You won't see the behavior you describe since each request will be handled synchronously. Enable threads or processes and you will see it.
app.run(threaded=True)
orapp.run(processes=10)
. (In 1.0 the server is threaded by default.)Some WSGI servers may support gevent or another async worker. Global variables are still not thread safe because there's still no protection against most race conditions. You can still have a scenario where one worker gets a value, yields, another modifies it, yields, then the first worker also modifies it.
If you need to store some global data during a request, you may use Flask's
g
object. Another common case is some top-level object that manages database connections. The distinction for this type of "global" is that it's unique to each request, not used between requests, and there's something managing the set up and teardown of the resource.