0

For example, I have a two tasks: a and b. I need to run them in parallel. I create a group of tasks and try run it. But I get error

proj/app/tasks.py

@app.task
def a():
    pass

@app.task
def b():
    pass

django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

Application app is registered in INSTALLED_APPS and all migration complited

proj/proj/__init__.py

from __future__ import absolute_import, unicode_literals
from .celery import app as celery_app

__all__ = ('celery_app',)

proj/proj/celery.py

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')

app = Celery('proj')
app.config_from_object('django.conf:settings', namespace='CELERY')

app.autodiscover_tasks()

@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))


from celery import group
from app.tasks import a, b

run_group = group(a.s(), b.s())
run_group()

Traceback

File "/home/m0nte-cr1st0/test_projects/proj/proj/__init__.py", line 5, in <module>
    from .celery import app as celery_app
File "/home/m0nte-cr1st0/test_projects/proj/proj/celery.py", line 26, in <module>
    from app.tasks import a, b
File "/home/m0nte-cr1st0/test_projects/proj/app/tasks.py", line 14, in <module>
    from .models import Link, Prediction, PredictionBK
File "/home/m0nte-cr1st0/test_projects/proj/app/models.py", line 2, in <module>
    from django.contrib.auth.models import AbstractUser
unknown
  • 252
  • 3
  • 12
  • 37

4 Answers4

1

Make sure you do the following:

  • reset your server

  • reset your celery worker pool

If that doesn't fix the problem let me know.

Also, you don't need the from __future__ import absolute_import, unicode_literals. This is only used for python2 compatibility.

Update

I just realized that you are importing them into your celery file (not sure if you can do this). Try deleting the task import, then try running the task in a django shell (run ./manage.py shell).

Lord Elrond
  • 13,430
  • 7
  • 40
  • 80
1

There are two problems here.

Firstly you should not be importing the other tasks - that is what app.autodiscover_tasks() is for. Remove the line from app.tasks import a, b

Secondly, you should not be calling run_group. That's for when you want to run the tasks in the group.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • but I want to run tasks in a group. – unknown Oct 10 '19 at 08:10
  • 1
    Yes. *Define* `run_group` there, as you are doing. But don't *call* it there. Call it when you want to run the group. – Daniel Roseman Oct 10 '19 at 08:14
  • Ok. Thank you for help. At now I get error `amqp.exceptions.AccessRefused: (0, 0): (403) ACCESS_REFUSED - Login was refused using authentication mechanism AMQPLAIN. For details see the broker logfile.` – unknown Oct 10 '19 at 08:18
  • It’s strange. After all, I use `redis`, and the error is related to `rabbitmq`? – unknown Oct 10 '19 at 08:20
1

proj/proj/celery.py

import os
from celery import Celery
from django.conf import settings

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')

app = Celery('proj')
app.config_from_object('django.conf:settings')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

proj/proj/settings.py

REDIS_HOST = 'localhost'
REDIS_PORT = '6379'
BROKER_URL = 'redis://' + REDIS_HOST + ':' + REDIS_PORT + '/0'
BROKER_TRANSPORT_OPTIONS = {'visibility_timeout': 3600}
CELERY_RESULT_BACKEND = 'redis://' + REDIS_HOST + ':' + REDIS_PORT + '/0'

proj/proj/__init__.py

from .celery import app as celery_app

proj/app/tasks.py

from celery import group


@app.task
def a():
    pass


@app.task
def b():
    pass


run_group = group(a.s(), b.s())
run_group()
unknown
  • 252
  • 3
  • 12
  • 37
-1

change your proj/proj/celery.py to this:


from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')

app = Celery('proj')
app.config_from_object('django.conf:settings')
app.autodiscover_tasks()

@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

import django
django.setup()

from celery import group
from app.tasks import a, b

run_group = group(a.s(), b.s())
run_group()