Django and AmqpConsumer with another apps

I have django apps and amqp consumer/publisher. I want to send an incoming message to my function in another application. But I get error messange: 'django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.' how can I do this?

My settings.INSTALLED_APPS:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'my_app',
    'rabbit_client.config.AmqpConfig'
]

AmqpConfig:

from django.apps import AppConfig
from rabbit_client.amqp_client import AMQPConsuming


class AmqpConfig(AppConfig):
    name = 'rabbit_client'

    def ready(self):
        consumer = AMQPConsuming()
        consumer.daemon = True
        consumer.start()

AMQPConsuming:

import threading

import pika
from django.conf import settings
from my_app.util import my_function   # it's call error messange


class AMQPConsuming(threading.Thread):
    def callback(self, channel, method, properties, body):
        data = body
        my_function(data)

    @staticmethod
    def _get_connection():
        parameters = pika.URLParameters(settings.RABBITMQ_SETTING['URL'])
        return pika.BlockingConnection(parameters)

    def run(self):
        connection = self._get_connection()
        channel = connection.channel()

        channel.queue_declare(
            queue=settings.RABBITMQ_SETTING['QUEUE_CONSUME'],
            durable=True
        )
        channel.basic_qos(prefetch_count=1)
        channel.basic_consume(
            on_message_callback=self.callback,
            queue=settings.RABBITMQ_SETTING['QUEUE_CONSUME']
        )
        channel.start_consuming()

This error may occur when you are adding an app in INSTALLED_APPS in the settings.py file but you do not have that app installed in your computer.

Back to Top