-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Flask contexts not available inside background callback #2235
Comments
For celery the |
@T4rk1n can you highlight why it doesn't work? In common, this function simply copies the current request context and provided it inside the function, like dash already doing with dash callback context. |
The main problem and thing that we need to remember – we should not try to push it when we're packing the function for celery, it should be passed to the prepared function as arguments (I mean wrapper that will call user's callback function). |
The issue is that wrapping the function in the callback where it should be, doesn't return a celery task object to call (Missing func.delay). The callback_context I refactored to use a context_var instead of the flask.g object for that matter. |
@T4rk1n so what do you think about it? (except of temp inability to serialize contextvars) def flask_patcher():
with flask.current_app.app_context():
@flask.copy_current_request_context
def _(fn, *args, **kwargs):
return fn(*args, **kwargs)
return _ def call_job_fn(self, key, job_fn, args, context):
task = job_fn.delay(key, self._make_progress_key(key), args, flask_patcher())
return task.task_id @celery_app.task(name=f"long_callback_{fn_hash}")
def job_fn(result_key, progress_key, user_callback_args, context=None, patcher=None): if isinstance(user_callback_args, dict):
user_callback_output = patcher(fn, *maybe_progress, **user_callback_args)
elif isinstance(user_callback_args, (list, tuple)):
user_callback_output = patcher(fn, *maybe_progress, *user_callback_args)
else:
user_callback_output = patcher(fn, *maybe_progress, user_callback_args) |
@ArtsiomAntropau If you can prove that works with a test, (can add |
From @JamesKunstle in #2636: Update: For anyone looking for a solution, you can pass the 'request=flask.request' object into background callbacks and that seems to work. https://community.plotly.com/t/how-to-read-cookies-inside-a-background-callback/70224/11 |
Hi @alexcjohnson ! So, this solution |
Describe your context
Describe the bug
Expected behavior
So, after creating callback function and before providing them to the Celery, we should provide flask contexts inside this function to imitate the default callback behaviour
Any recommendations for now?
The text was updated successfully, but these errors were encountered: