1

This is bugging me for past few hours and I can not seem to find a solution yet.

I am using django-rq to enqueue some long running tasks. In my tasks.py, I have the following:

from django_rq import job
@job
def long_running_task(hash, url, file_path):

     #doing some work

and in my views.py,

def post(self, request, hash, file_path, format=None):
    URL = "http://127.0.0.1:9000/work/"
    task = django_rq.enqueue(long_running_task, hash, URL, file_path)
    print("job result is: ", task.result)
    return JsonResponse({"task_result": task.result})

When I run it, however, it fails with the following message-

TypeError: long_running_task() takes 2 positional arguments but 3 were given

Clearly, I am doing something silly here, but I am not able to figure it out yet. Can someone please let me know what's going on here?

Subzero
  • 841
  • 6
  • 20

2 Answers2

0

What happens when you remove the decorator?

AFAIU, enqueue just takes a function and its args and returns a job. The decorator declares a function as a job. You need to use one or the other.

To use w/ the decorator you'll need to do something like the following pseudo:

def post(self, request, hash, file_path, format=None):
    URL = "http://127.0.0.1:9000/work/"
    task = long_running_task.delay(hash, URL, file_path)
    # wait some time for completion
    print("job result is: ", task.result)
    return JsonResponse({"task_result": task.result})
Itamar Haber
  • 47,336
  • 7
  • 91
  • 117
  • a) Even If I remove the @job decorator, I still get the same error. b) If I keep the decorator and use the .delay, I still get the same error. – Subzero Aug 27 '17 at 07:20
0

Strangely, this seemed to be a circular dependency type of issue. Previously, I had the tasks.py file (where I defined the long_running_task) one level up from my views.py file. I moved the tasks.py file to the same level and it started working fine. No idea though as to why the seemingly unrelated error was being thrown.

Subzero
  • 841
  • 6
  • 20
  • Old, but I already had the files on the same level. However, as soon as I switched from passing a function object to a string for the function, it worked. Unsure why. – chr0nikler Mar 06 '20 at 06:21