flask-executor
flask-executor copied to clipboard
Executor map breaking in Flask==2.2.2
Getting this error when getting the return from executor.map()
:
ValueError: <Token var=<ContextVar name='flask.request_ctx' at 0x7f69fc5df3d0> at 0x7f69e7967780> was created in a different Context
My configs:
EXECUTOR_TYPE = 'thread'
EXECUTOR_MAX_WORKERS = 4
EXECUTOR_PUSH_APP_CONTEXT = True
update executor version to 1.0.0. pypi already release. and set config "EXECUTOR_PUSH_APP_CONTEXT = True"
update executor version to 1.0.0. pypi already release. and set config "EXECUTOR_PUSH_APP_CONTEXT = True"
It did not help me
I encountered the same problem.
I have what appears to be the same issue. If I have 2 or more threads I get the same message, Also running 1.0.0.
ValueError: <Token var=<ContextVar name='flask.request_ctx' at 0x7f053937c720> at 0x7f05337bc3c0> was created in a different Context
What tasks are you performing in your executors? In my case I have the executors performing web API calls, so there is quite a bit going on inside each of the executor tasks and it takes a second or so for each to complete.
I found a few things, if I make a dummy function that doesn't do anything except just print and return it works fine, regardless of how many executors I configure.
If I run only 1 thread it works fine with the big web API call function. If I run 2 threads the first one completes and the second one has the issue.
I may have found a fix, I don't understand it, but it works for me.
Edit executor.py and try making copy_current_request_context
optional with the flag set to False
or just comment that line out. I implemented the changes in this feature request https://github.com/dchevell/flask-executor/issues/52, disabling copy_current_request_context
and my issue went away.
I added a few print statements and captured some debug information. Does this shed light on a cause of the issue?
Debug output of working code
[remote MyIP:MyPort] Application EXECUTOR Configuration
[remote MyIP:MyPort] EXECUTOR_TYPE=thread
[remote MyIP:MyPort] EXECUTOR_MAX_WORKERS=20
[remote MyIP:MyPort] EXECUTOR_FUTURES_MAX_LENGTH=21
[remote MyIP:MyPort] EXECUTOR_PROPAGATE_EXCEPTIONS=True
[remote MyIP:MyPort] EXECUTOR_PUSH_APP_CONTEXT=True
[remote MyIP:MyPort] EXECUTOR_PUSH_REQUEST_CONTEXT=False
[remote MyIP:MyPort] Hello from _prepare_fn
[remote MyIP:MyPort] Pushing app context in _prepare_fn
[remote MyIP:MyPort] Before copy app context, repr(fn)=<bound method MyObj.MyFunction of <MyLibary.MyObj object at 0x7f0a41855f30>>
[remote MyIP:MyPort] Before copy app context, vars(fn)={}
[remote MyIP:MyPort] Hello from push_app_context
[remote MyIP:MyPort] After copy app context, repr(fn)=<function push_app_context.<locals>.wrapper at 0x7f0a40bd7010>
[remote MyIP:MyPort] After copy app context, vars(fn)={}
Hello from push_app_context wrapper
Hello from push_app_context wrapper
. . .
Completed without issue
Debug output of non-working code
[remote MyIP:MyPort] Application EXECUTOR Configuration
[remote MyIP:MyPort] EXECUTOR_TYPE=thread
[remote MyIP:MyPort] EXECUTOR_MAX_WORKERS=20
[remote MyIP:MyPort] EXECUTOR_FUTURES_MAX_LENGTH=21
[remote MyIP:MyPort] EXECUTOR_PROPAGATE_EXCEPTIONS=True
[remote MyIP:MyPort] EXECUTOR_PUSH_APP_CONTEXT=True
[remote MyIP:MyPort] EXECUTOR_PUSH_REQUEST_CONTEXT=True
[remote MyIP:MyPort] Hello from _prepare_fn
[remote MyIP:MyPort] Pushing request context in _prepare_fn
[remote MyIP:MyPort] Before copy request context, repr(fn)=<bound method MyObj.MyFunction of <MyLibary.MyObj object at 0x7ff624489f30>>
[remote MyIP:MyPort] Before copy request context, vars(fn)={}
[remote MyIP:MyPort] After copy request context, repr(fn)=<function MyObj.MyFunction at 0x7ff61f72f010>
[remote MyIP:MyPort] After copy request context, vars(fn)={'__wrapped__': <bound method MyObj.MyFunction of <MyLibary.MyObj object at 0x7ff624489f30>>}
[remote MyIP:MyPort] Pushing app context in _prepare_fn
[remote MyIP:MyPort] Before copy app context, repr(fn)=<function MyObj.MyFunction at 0x7ff61f72f010>
[remote MyIP:MyPort] Before copy app context, vars(fn)={'__wrapped__': <bound method MyObj.MyFunction of <MyLibary.MyObj object at 0x7ff624489f30>>}
[remote MyIP:MyPort] Hello from push_app_context
[remote MyIP:MyPort] After copy app context, repr(fn)=<function push_app_context.<locals>.wrapper at 0x7ff61ef74430>
[remote MyIP:MyPort] After copy app context, vars(fn)={}
Hello from push_app_context wrapper
Hello from push_app_context wrapper
ValueError: <Token var=<ContextVar name='flask.request_ctx' at 0x7fefc2d874c0> at 0x7fefc11bec40> was created in a different Context