djangodjango-channelsdaphneasgi

Asgi:Daphne Django issue in production


Hey i have error in my project its kind of wired error because the code works totally fine locally but its not working when i deploy it on Railway i installed channels[Daphne] and created web-socket and it works great but on deploy it gives this error

2023-10-04 19:10:30,487 INFO     /app/.env not found - if you're not configuring your environment separately, check this.
Traceback (most recent call last):
File "/opt/venv/bin/daphne", line 8, in <module>
sys.exit(CommandLineInterface.entrypoint())
File "/opt/venv/lib/python3.10/site-packages/daphne/cli.py", line 171, in entrypoint
cls().run(sys.argv[1:])
File "/opt/venv/lib/python3.10/site-packages/daphne/cli.py", line 233, in run
application = import_by_path(args.application)
File "/opt/venv/lib/python3.10/site-packages/daphne/utils.py", line 12, in import_by_path
target = importlib.import_module(module_path)
File "/root/.nix-profile/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/app/./project/asgi.py", line 1, in <module>
import notification.routing
File "/app/notification/routing.py", line 2, in <module>
from notification.consumer import NotificationConsumer
File "/app/notification/consumer.py", line 7, in <module>
from notification.models import Notification
File "/app/notification/models.py", line 3, in <module>
from posts.models import PublishedPost
File "/app/posts/models.py", line 2, in <module>
from django.contrib.auth.models import User
File "/opt/venv/lib/python3.10/site-packages/django/contrib/auth/models.py", line 3, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/opt/venv/lib/python3.10/site-packages/django/contrib/auth/base_user.py", line 49, in <module>
class AbstractBaseUser(models.Model):
File "/opt/venv/lib/python3.10/site-packages/django/db/models/base.py", line 127, in __new__
app_config = apps.get_containing_app_config(module)
File "/opt/venv/lib/python3.10/site-packages/django/apps/registry.py", line 260, in get_containing_app_config
self.check_apps_ready()
File "/opt/venv/lib/python3.10/site-packages/django/apps/registry.py", line 138, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

i tried to check my packages if there is different than the req.txt but its all same and created a runtime.txt and i don't know where to check the error because its working fine locally


Solution

  • Quick Answer

    I cannot see your code, but I have faced similar import issues when moving over to ASGI from WSGI. I think this can be solved with how you are doing your imports

    from django.core.asgi import get_asgi_application
    
    # Initialize Django ASGI application early to ensure the AppRegistry
    # is populated before importing code that may import ORM models.
    django_asgi_app = get_asgi_application()
    
    from posts.models import PublishedPost
    from ... import ...
    from ... import ...
    

    Side note

    In general I suggest using uvicorn and gunicorn over daphne for ASGI applications. They are more lightweight and, in my opinion, more robust than django's default counter-part.