Mensur
Mensur

Reputation: 1096

How to keep a request context in a celery task, in Python Flask?

Is there a way to copy the request to a celery task in Flask in such a manner that the task executes inside the request context which initiated the task?
I need to access the flask security current user in a celery task, but since the task is outside the request context, I can not do that. I need additional information from the request, so just forwarding the current user to the task would not do the trick.

My task does inserts on the database. It needs the current user to save the id of the user which creates the row. Passing the user object to the task would solve the problem. However, the application logic is such that every insert/delete/update is logged via before flush event, which logs the user who did the modification, his IP, original url, the data it inserts...)

Log event is done like I said before flush, and it works in 99% scenarios. But when I have one lengthy task which I want to be a celery task, the request data is not available, nor is the current user (since it is outside the original request context)

Upvotes: 9

Views: 3137

Answers (1)

Arunmozhi
Arunmozhi

Reputation: 964

The is no out-of-the-box way to pass the request or current_user objects to the celery task as they are non serializable. But people have worked around this by creating a wrapper to call the celery task in the request context.

The Celery task in a Flask request context blog post explores this topic in detail. The gists by Xion and derived one by aviaryan with the Request Context wrapper.

Upvotes: 3

Related Questions