passing django request object to celery task

后端 未结 1 1488
故里飘歌
故里飘歌 2020-12-19 08:21

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

相关标签:
1条回答
  • 2020-12-19 08:52

    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})
    
    0 讨论(0)
提交回复
热议问题