Django/Celery 'Received unregistered task of type'

Viewed 168

I have hit a brick wall for quite some time with my simple task that I need to run asynchronously from a Django server. I have a form on a page that when I submit a value my views.py executes a simple multiplication function from the tasks2.py file which also has my Celery stuff.

With Celery running by this command from my terminal:


    celery -A tasks2 worker -l info -P eventlet

And submitting a number "2 in this case" through my form I get the following error


    Received unregistered task of type 'app_scrape.tasks2.my_multiply'.
    The message has been ignored and discarded.
    Or maybe you're using relative imports?
    
    Please see
    http://docs.celeryq.org/en/latest/internals/protocol.html
    for more information.
    
    The full contents of the message body was:
    '[["2"], {}, {"callbacks": null, "errbacks": null, "chain": null, "chord": null}]' (80b)
    Traceback (most recent call last):                                         null}]' (80b)
      File "c:\users\likea\.virtualenvs\django-j1eue__k\lib\site-packages\celery\worker\consumer\consumer.py", line 581, in on_task_received            ry\worker\consumer\consumer.py", line 581, in on_task_received
        strategy = strategies[type_]
    KeyError: 'app_scrape.tasks2.my_multiply'

Project structure:


     - src-estategy
     -   manage.py
     -   app_scrape
     -      views.py
     -      tasks2.py
     -      models.py
     -      ....
     -   proj_estategy
     -      __init__.py
     -      settings.py
     -      urls.py
     -      ....

tasks2.py


    from celery import Celery
    
    app = Celery('tasks2', broker='amqps://mcbojxac:6sC7n38IdqmNoNgUjmjBkHum6RxRnaqt@clam.rmq.cloudamqp.com/mcbojxac', backend='db+postgresql://kbsrjinigbkzqu:ed0aab74c997b56aa756d510439e28404cf1e87de629a7af66bd49ae29961bbb@ec2-44-195-201-3.compute-1.amazonaws.com/d85tdjbd3am2sc')
    
    @app.task
    def my_multiply(x):
        print(x)
        return x
    ```
    **views.py**
    ```
    #Django
    from django.shortcuts import render, redirect
    
    # Form for input into my_multiply
    from .forms import UserQueryForm
    
    #Celery
    from .tasks2 import my_multiply
    
    def scrape_view(request):
        form = UserQueryForm(request.POST or None)
        if form.is_valid():
            form.save()
            data = form.cleaned_data['my_number']
            print("success")
            my_multiply.delay(data)
               
        context = {
            'form': form
        }
        return render(request, 'scrape.html', context)

I figured this might be a Django/Celery import problem so I tried moving all the code from tasks2.py directly into the views.py to prevent any import issues, but then by running


    celery -A views worker -l info -P eventlet

command I get this error

 

     File "C:\Users\likea\Documents\Coding\django\src-estategy\app_scrape\views.py", line 5, in <module>
        from .forms import UserQueryForm
    ImportError: attempted relative import with no known parent package

For reference here is forms.py


    from django import forms
    from .models import UserQuery
    
    class UserQueryForm(forms.ModelForm):
        class Meta:
            model = UserQuery
            fields = [
                'my_number',
                'my_boolean'
            ]

I know this is not an uncommon error and I have scoured all the relevant questions asked here but none of the solutions have helped me, I have had this problem for weeks now. What could be wrong with my code?

1 Answers

Had so many issues with Celery, eventually tried the approach on Linux and got a bit further, but then had issues reading environment variables that were configured on Heroku. Eventually tried rq with the official Heroku instructions and had the app working perfectly.

Related