M
M
Maxim2018-01-12 11:27:22
Django
Maxim, 2018-01-12 11:27:22

Why is the import not running?

The project worked fine, until it was necessary to introduce periodic tasks.
Since the decorator is removed from the functionality, I use this

# coding: utf8
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from api.controllers.message_scheduled import scheduled


# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings')

app = Celery('backend')

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()

# Redis
app.conf.broker_url = 'redis://redis:6379/0'

# Enable events
# app.control.enable_events()


@app.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
    # sender.add_periodic_task(60.0, message_scheduled.s(), name='Scheduled message')
    sender.add_periodic_task(10.0, scheduled.s(), name='add every 10')


    # # Calls test('hello') every 10 seconds.
    # sender.add_periodic_task(10.0, test.s('hello'), name='add every 10')
    #
    # # Calls test('world') every 30 seconds
    # sender.add_periodic_task(30.0, test.s('world'), expires=10)
    #
    # # Executes every Monday morning at 7:30 a.m.
    # sender.add_periodic_task(
    #     crontab(hour=7, minute=30, day_of_week=1),
    #     test.s('Happy Mondays!'),
    # )

@app.task
def test(arg):
    print(arg)

In this case I get an error
web_1        |   File "/usr/local/lib/python3.4/site-packages/django/apps/registry.py", line 239, in get_containing_app_config
web_1        |     self.check_apps_ready()
web_1        |   File "/usr/local/lib/python3.4/site-packages/django/apps/registry.py", line 124, in check_apps_ready
web_1        |     raise AppRegistryNotReady("Apps aren't loaded yet.")
web_1        | django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

How to import functions from django app?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2018-01-12
@maximkv25

In the celery settings, add periodic tasks as follows

# Celery beat
app.conf.beat_schedule = {
    'scheduled': {
        'task': 'api.controllers.message.scheduled_message',
        'schedule': 60.0
    }
}

app.conf.timezone = 'UTC'

# scheduled_message оборачиваем в обычный task
@task()
def scheduled_message():
    pass

Regarding import, the issue remains relevant and if anyone knows what the reason is, I will be grateful for the clarification.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question