Replies: 4 comments
-
I am facing the same issue after upgrading flask to v 2.2.5 (python 3.11) |
Beta Was this translation helpful? Give feedback.
-
Interesting. not working: @copy_current_request_context
def do_something(param):
pass
loop.run_in_executor(
executor,
do_something
param
) working: def do_something(param):
pass
loop.run_in_executor(
executor,
copy_current_request_context(do_something)
param
) after removing |
Beta Was this translation helpful? Give feedback.
-
I'm also encountering this issue, and neither approaches above worked for me. I'm on flask v 2.3.3 on Python 3.11. Were you able to get that to work consistently @danderwald? |
Beta Was this translation helpful? Give feedback.
-
Hi all, I encountered the same issue and found the root cause, so I post it here for others also hitting it. Reason for that: copy_current_request_context is executed only once in the current thread and since copy_current_request_context creates the copy of the request context when the decorator is applied and not when the function is called, the same request context is used in all threads leading to the issue above. In order to fix it, you need to perform the copy in each worker thread. Saying this, copy_current_request_context cannot be used on a function which is passed into a thread pool |
Beta Was this translation helpful? Give feedback.
-
I want to use the main thread's request context in the sub-thread. So I use the decorator copy_current_request_context. But I got an error that <Token var=<ContextVar name='flask.request_ctx' at 0x1067aef90> at 0x106c71280> was created in a different Context.
The weirdest thing is when I debug the code one by one, I will get the expected results sometimes.
This is a simple code.
Environment:
Beta Was this translation helpful? Give feedback.
All reactions