Home > Back-end >  Access Requests Method within Celery Task
Access Requests Method within Celery Task

Time:11-09

Is it possible to access the requests.POST/GET methods within a celery task that's in a django project? I've read that it's not possible because celery can't serialized the requests JSON objects. Other than taking the data from the requests.POST['data'] object and passing them to the celery task, are there any other work arounds?

def index(request):
    task = run_tasks.delay(request) # I would like to pass the request data to the task
    return render(request, 'example/index.html', {'task_id': task.task_id})

CodePudding user response:

You can work with .urlencode(…) [Django-doc] to convert it to a string object and back:

def index(request):
    run_tasks.delay(data=request.GET.urlencode())
    return render(request, 'example/index.html', {'task_id': task.task_id})

at the receiving end, you can then reconstruct the QueryDict with:

from django.http import QueryDict

def some_task(data):
    GET = QueryDict(data)
    # work with GET …
    pass
  • Related