How to save request context in celery task, in Python Flask?

Is there a way to copy a request to a celery task in Flask so that the task runs inside the context of the request that initiated the task?

I need to access the current flash security user in a celery task, but since the task is outside the request context, I cannot do this. I need more information from the request, so just forwarding the current user to the task will not do the trick.

My task inserts into the database. The user needs to save the user ID that creates the string. Passing the user object to the task will solve the problem. However, the application logic is such that each insert / delete / update is registered through the flush event, which registers the user who made the changes, his IP, original URL, the data that he inserts ...)

The log event is executed, as I said, before starting, and works in 99% of the scenarios. But when I have one long task that I want to make celery, the request data is not available, as well as the current user (because it is outside the original request context)

+4
source share
1 answer

current_user celery, . , .

. Gists Xion aviaryan .

+1

Source: https://habr.com/ru/post/1526751/


All Articles