Reputation: 1
We are trying to use django channels to implement websockets in a multi-tenant django application.
I have followed the tutorial page in the django channels documentation and also applied the instructions found in the deploying page. However, I am seeing the django.core.exceptions.AppRegistryNotReady error when running pytest cases or even when running a simple "python manage.py check".
django@fa5404e77654:/usr/src/app$ python manage.py check
Traceback (most recent call last):
File "manage.py", line 36, in <module>
main()
File "manage.py", line 32, in main
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 357, in execute
django.setup()
File "/usr/local/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python3.8/site-packages/django/apps/registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "/usr/local/lib/python3.8/site-packages/django/apps/config.py", line 116, in create
mod = import_module(mod_path)
File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/usr/local/lib/python3.8/site-packages/channels/apps.py", line 4, in <module>
import daphne.server
File "/usr/local/lib/python3.8/site-packages/daphne/server.py", line 7, in <module>
twisted_loop = asyncio.new_event_loop()
File "/usr/local/lib/python3.8/asyncio/events.py", line 758, in new_event_loop
return get_event_loop_policy().new_event_loop()
File "/usr/local/lib/python3.8/asyncio/events.py", line 656, in new_event_loop
return self._loop_factory()
File "/usr/local/lib/python3.8/asyncio/unix_events.py", line 54, in __init__
super().__init__(selector)
File "/usr/local/lib/python3.8/asyncio/selector_events.py", line 59, in __init__
logger.debug('Using selector: %s', selector.__class__.__name__)
File "/usr/local/lib/python3.8/logging/__init__.py", line 1422, in debug
self._log(DEBUG, msg, args, **kwargs)
File "/usr/local/lib/python3.8/logging/__init__.py", line 1577, in _log
self.handle(record)
File "/usr/local/lib/python3.8/logging/__init__.py", line 1587, in handle
self.callHandlers(record)
File "/usr/local/lib/python3.8/logging/__init__.py", line 1649, in callHandlers
hdlr.handle(record)
File "/usr/local/lib/python3.8/logging/__init__.py", line 946, in handle
rv = self.filter(record)
File "/usr/local/lib/python3.8/logging/__init__.py", line 807, in filter
result = f.filter(record)
File "/usr/local/lib/python3.8/site-packages/django_tenants/log.py", line 12, in filter
record.schema_name = connection.tenant.schema_name
File "/usr/local/lib/python3.8/site-packages/django/db/__init__.py", line 28, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", line 201, in __getitem__
backend = load_backend(db['ENGINE'])
File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", line 110, in load_backend
return import_module('%s.base' % backend_name)
File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/usr/local/lib/python3.8/site-packages/django_tenants/postgresql_backend/base.py", line 8, in <module>
from django.contrib.contenttypes.models import ContentType
File "/usr/local/lib/python3.8/site-packages/django/contrib/contenttypes/models.py", line 133, in <module>
class ContentType(models.Model):
File "/usr/local/lib/python3.8/site-packages/django/db/models/base.py", line 103, in __new__
app_config = apps.get_containing_app_config(module)
File "/usr/local/lib/python3.8/site-packages/django/apps/registry.py", line 252, in get_containing_app_config
self.check_apps_ready()
File "/usr/local/lib/python3.8/site-packages/django/apps/registry.py", line 135, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
Exception ignored in: <function BaseEventLoop.__del__ at 0x7f3938e06ca0>
Traceback (most recent call last):
File "/usr/local/lib/python3.8/asyncio/base_events.py", line 656, in __del__
self.close()
File "/usr/local/lib/python3.8/asyncio/unix_events.py", line 58, in close
super().close()
File "/usr/local/lib/python3.8/asyncio/selector_events.py", line 92, in close
self._close_self_pipe()
File "/usr/local/lib/python3.8/asyncio/selector_events.py", line 99, in _close_self_pipe
self._remove_reader(self._ssock.fileno())
AttributeError: '_UnixSelectorEventLoop' object has no attribute '_ssock'
I am not sure if there are some compatibility issues between the packages I have in the INSTALLED_APPS of django settings, but putting "channels" in the INSTALLED_APPS causes the error.
We use docker to run the application. I've also modified the startup script to use daphne instead of gunicorn. Then I run my test cases from inside the django container in docker.
Here's how my files look like:
asgi.py
import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "arkis.settings")
django.setup()
from channels.auth import AuthMiddlewareStack
from channels.http import AsgiHandler
from channels.routing import ProtocolTypeRouter, URLRouter
from arkis.integrations.wizard.routing import websocket_urlpatterns
application = ProtocolTypeRouter(
{"http": AsgiHandler(), "websocket": AuthMiddlewareStack(URLRouter(websocket_urlpatterns))}
)
routing.py
from django.urls import re_path
from arkis.integrations.wizard.consumers import WizardListConsumer
websocket_urlpatterns = [
re_path(r"ws/integrations/(?P<integration_name>)/wizard/list/$", WizardListConsumer.as_asgi())
]
Here are the version details:
Looking at the stack trace, I suppose the error lies in the django tenants but I have no idea where to proceed from here.
Upvotes: 0
Views: 693
Reputation: 1
The error is because you are trying to access some models and for some reasons the app who has those models hasn't been loaded yet.
To solve that try to add these before anything in your consumer.py as well as your asgi.py
import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "arkis.settings")
django.setup()
To know more you can read How applications are loaded
Upvotes: 0