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

白昼怎懂夜的黑 提交于 2019-12-30 19:26:16

问题


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)


回答1:


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.



来源:https://stackoverflow.com/questions/31383953/how-to-keep-a-request-context-in-a-celery-task-in-python-flask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!