You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
copy_current_request_context creates new global app context #3306
When I use @copy_current_request_context it should preserve the contents in flask.g.
I would expect that the g object is transparent to whether I'm in a normal request context, or one that has been copied using the copy_current_request_context decorator in another greenlet.
I borrowed the very nice test-script from #2935 and adapted it for g instead.
I can see why it's confusing, but I'm not sure how we would change this. You copied the request context, not the app context. Pushing a request context pushes an app context if there isn't one, and that's what g is tied to.
Confusing indeed. What is the purpose of the app context, then?
How are you supposed to use it, that isn't solved by using the request context?
I've adapted, and now use the request context rather than the app context, but it wasn't really clear to me that we could use the request context for storing arbitrary data. Also, it lacks some of the get/set methods present on g, adding to the assumption that g is the object to put your data in, rather than the request context.
Wouldn't it make sense to keep the same app context, also for the newly pushed (copied) request context?
@kaos for what it's worth, I discovered this when I was raising #2935 and g being bound to the app context caught be by surprise too. I haven't found a great solution, because pushing/copying the app context a) doesn't have a built in decorator (leading me to believe it's not something we're expected to be doing) and b) tends to have weird side effects when used in combination with important extensions like Flask-SQLAlchemy.
I'm currently trying to untangle one of these side effects in my own Flask-Executor extension: dchevell/flask-executor#15. FWIW, that extension does what you're trying to do automatically - by pushing the app context with every request it supports accessing g from threads, and I suspect your example would succeed there.
Assuming this is closed as "not a bug", perhaps you could pop over there and see if it solves your problem.
Activity
davidism commentedon Jul 23, 2019
I can see why it's confusing, but I'm not sure how we would change this. You copied the request context, not the app context. Pushing a request context pushes an app context if there isn't one, and that's what
gis tied to.kaos commentedon Jul 24, 2019
Confusing indeed. What is the purpose of the app context, then?
How are you supposed to use it, that isn't solved by using the request context?
I've adapted, and now use the request context rather than the app context, but it wasn't really clear to me that we could use the request context for storing arbitrary data. Also, it lacks some of the get/set methods present on
g, adding to the assumption thatgis the object to put your data in, rather than the request context.Wouldn't it make sense to keep the same app context, also for the newly pushed (copied) request context?
dchevell commentedon Sep 29, 2019
@kaos for what it's worth, I discovered this when I was raising #2935 and
gbeing bound to the app context caught be by surprise too. I haven't found a great solution, because pushing/copying the app context a) doesn't have a built in decorator (leading me to believe it's not something we're expected to be doing) and b) tends to have weird side effects when used in combination with important extensions like Flask-SQLAlchemy.I'm currently trying to untangle one of these side effects in my own Flask-Executor extension: dchevell/flask-executor#15. FWIW, that extension does what you're trying to do automatically - by pushing the app context with every request it supports accessing
gfrom threads, and I suspect your example would succeed there.Assuming this is closed as "not a bug", perhaps you could pop over there and see if it solves your problem.
kaos commentedon Dec 30, 2019
Thanks @dchevell will have a look at what you have done.
Closing as "not a bug", seeing that we should rely on
flask.requestrather than ongfor request time globals.