flask.g and flask.request are thread local storage?
My understanding is that flask.g object or flask.request are the thread local storage.
However when I execute the following code, it shows the id of flask.g (and flask.request) is always same value in each threads:
from flask import Flask, request, g
app = Flask(__name__)
@app.route('/')
def hello():
print("g id: %d" % id(g))
print("request id: %d" % id(request))
return "Hello world"
if __name__ == "__main__":
app.run()
result (access three times with several browsers):
g id: 140219657264584
request id: 140219657262640
g id: 140219657264584
request id: 140219657262640
g id: 140219657264584
request id: 140219657262640
My understanding iswrong?
python flask
add a comment |
My understanding is that flask.g object or flask.request are the thread local storage.
However when I execute the following code, it shows the id of flask.g (and flask.request) is always same value in each threads:
from flask import Flask, request, g
app = Flask(__name__)
@app.route('/')
def hello():
print("g id: %d" % id(g))
print("request id: %d" % id(request))
return "Hello world"
if __name__ == "__main__":
app.run()
result (access three times with several browsers):
g id: 140219657264584
request id: 140219657262640
g id: 140219657264584
request id: 140219657262640
g id: 140219657264584
request id: 140219657262640
My understanding iswrong?
python flask
By default, flask only use one thread, so every request you sent are hitting that same one thread. Now if you add a background thread things will be different (also please refer to that question's related question on the sidebar).
– metatoaster
Nov 16 '18 at 2:11
They are proxy objects for convenient access to thread-local variables.
– Klaus D.
Nov 16 '18 at 3:36
add a comment |
My understanding is that flask.g object or flask.request are the thread local storage.
However when I execute the following code, it shows the id of flask.g (and flask.request) is always same value in each threads:
from flask import Flask, request, g
app = Flask(__name__)
@app.route('/')
def hello():
print("g id: %d" % id(g))
print("request id: %d" % id(request))
return "Hello world"
if __name__ == "__main__":
app.run()
result (access three times with several browsers):
g id: 140219657264584
request id: 140219657262640
g id: 140219657264584
request id: 140219657262640
g id: 140219657264584
request id: 140219657262640
My understanding iswrong?
python flask
My understanding is that flask.g object or flask.request are the thread local storage.
However when I execute the following code, it shows the id of flask.g (and flask.request) is always same value in each threads:
from flask import Flask, request, g
app = Flask(__name__)
@app.route('/')
def hello():
print("g id: %d" % id(g))
print("request id: %d" % id(request))
return "Hello world"
if __name__ == "__main__":
app.run()
result (access three times with several browsers):
g id: 140219657264584
request id: 140219657262640
g id: 140219657264584
request id: 140219657262640
g id: 140219657264584
request id: 140219657262640
My understanding iswrong?
python flask
python flask
asked Nov 16 '18 at 2:05
ryo.fastriverryo.fastriver
1
1
By default, flask only use one thread, so every request you sent are hitting that same one thread. Now if you add a background thread things will be different (also please refer to that question's related question on the sidebar).
– metatoaster
Nov 16 '18 at 2:11
They are proxy objects for convenient access to thread-local variables.
– Klaus D.
Nov 16 '18 at 3:36
add a comment |
By default, flask only use one thread, so every request you sent are hitting that same one thread. Now if you add a background thread things will be different (also please refer to that question's related question on the sidebar).
– metatoaster
Nov 16 '18 at 2:11
They are proxy objects for convenient access to thread-local variables.
– Klaus D.
Nov 16 '18 at 3:36
By default, flask only use one thread, so every request you sent are hitting that same one thread. Now if you add a background thread things will be different (also please refer to that question's related question on the sidebar).
– metatoaster
Nov 16 '18 at 2:11
By default, flask only use one thread, so every request you sent are hitting that same one thread. Now if you add a background thread things will be different (also please refer to that question's related question on the sidebar).
– metatoaster
Nov 16 '18 at 2:11
They are proxy objects for convenient access to thread-local variables.
– Klaus D.
Nov 16 '18 at 3:36
They are proxy objects for convenient access to thread-local variables.
– Klaus D.
Nov 16 '18 at 3:36
add a comment |
1 Answer
1
active
oldest
votes
I think g and request is the variable object.So the id will always equal.
When in g to set data
g = {}
g[current thread id] = your data
When in g to get data
g[current thread id]
So flask.g and flask.request are thread local storage
the g point is not to be changed, so id(g) is equivalent
– jiangyx3915
Nov 16 '18 at 2:25
Thank you for your answer. I understand g is global variable itself. However I can put stuff in it and can get back later from the one in a thread-safe. The following doc says that: http://werkzeug.pocoo.org/docs/0.14/local/
– ryo.fastriver
Nov 16 '18 at 4:25
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53330451%2fflask-g-and-flask-request-are-thread-local-storage%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think g and request is the variable object.So the id will always equal.
When in g to set data
g = {}
g[current thread id] = your data
When in g to get data
g[current thread id]
So flask.g and flask.request are thread local storage
the g point is not to be changed, so id(g) is equivalent
– jiangyx3915
Nov 16 '18 at 2:25
Thank you for your answer. I understand g is global variable itself. However I can put stuff in it and can get back later from the one in a thread-safe. The following doc says that: http://werkzeug.pocoo.org/docs/0.14/local/
– ryo.fastriver
Nov 16 '18 at 4:25
add a comment |
I think g and request is the variable object.So the id will always equal.
When in g to set data
g = {}
g[current thread id] = your data
When in g to get data
g[current thread id]
So flask.g and flask.request are thread local storage
the g point is not to be changed, so id(g) is equivalent
– jiangyx3915
Nov 16 '18 at 2:25
Thank you for your answer. I understand g is global variable itself. However I can put stuff in it and can get back later from the one in a thread-safe. The following doc says that: http://werkzeug.pocoo.org/docs/0.14/local/
– ryo.fastriver
Nov 16 '18 at 4:25
add a comment |
I think g and request is the variable object.So the id will always equal.
When in g to set data
g = {}
g[current thread id] = your data
When in g to get data
g[current thread id]
So flask.g and flask.request are thread local storage
I think g and request is the variable object.So the id will always equal.
When in g to set data
g = {}
g[current thread id] = your data
When in g to get data
g[current thread id]
So flask.g and flask.request are thread local storage
answered Nov 16 '18 at 2:23
jiangyx3915jiangyx3915
463
463
the g point is not to be changed, so id(g) is equivalent
– jiangyx3915
Nov 16 '18 at 2:25
Thank you for your answer. I understand g is global variable itself. However I can put stuff in it and can get back later from the one in a thread-safe. The following doc says that: http://werkzeug.pocoo.org/docs/0.14/local/
– ryo.fastriver
Nov 16 '18 at 4:25
add a comment |
the g point is not to be changed, so id(g) is equivalent
– jiangyx3915
Nov 16 '18 at 2:25
Thank you for your answer. I understand g is global variable itself. However I can put stuff in it and can get back later from the one in a thread-safe. The following doc says that: http://werkzeug.pocoo.org/docs/0.14/local/
– ryo.fastriver
Nov 16 '18 at 4:25
the g point is not to be changed, so id(g) is equivalent
– jiangyx3915
Nov 16 '18 at 2:25
the g point is not to be changed, so id(g) is equivalent
– jiangyx3915
Nov 16 '18 at 2:25
Thank you for your answer. I understand g is global variable itself. However I can put stuff in it and can get back later from the one in a thread-safe. The following doc says that: http://werkzeug.pocoo.org/docs/0.14/local/
– ryo.fastriver
Nov 16 '18 at 4:25
Thank you for your answer. I understand g is global variable itself. However I can put stuff in it and can get back later from the one in a thread-safe. The following doc says that: http://werkzeug.pocoo.org/docs/0.14/local/
– ryo.fastriver
Nov 16 '18 at 4:25
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53330451%2fflask-g-and-flask-request-are-thread-local-storage%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
By default, flask only use one thread, so every request you sent are hitting that same one thread. Now if you add a background thread things will be different (also please refer to that question's related question on the sidebar).
– metatoaster
Nov 16 '18 at 2:11
They are proxy objects for convenient access to thread-local variables.
– Klaus D.
Nov 16 '18 at 3:36