passing django request object to celery task

浪尽此生 提交于 2019-11-28 04:06:03

问题


I have a task in tasks.py like so:

@app.task
def location(request):
....

I am trying to pass the request object directly from a few to task like so:

def tag_location(request):
    tasks.location.delay(request)
    return JsonResponse({'response': 1})

I am getting an error that it can't be serialized i guess? How do I fix this? trouble is I have file upload objects as well .. its not all simple data types.


回答1:


Because the request object contains references to things which aren't practical to serialize — like uploaded files, or the socket associated with the request — there's no general purpose way to serialize it.

Instead, you should just pull out and pass the portions of it that you need. For example, something like:

import tempfile

@app.task
def location(user_id, uploaded_file_path):
    # … do stuff …

def tag_location(request):
    with tempfile.NamedTemporaryFile(delete=False) as f:
        for chunk in request.FILES["some_file"].chunks():
            f.write(chunk)
    tasks.location.delay(request.user.id, f.name)
    return JsonResponse({'response': 1})


来源:https://stackoverflow.com/questions/31304095/passing-django-request-object-to-celery-task

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