0

使用通道 asgi 开发服务器运行我的项目python manage.py runserver可以完美地启动它,但是当使用 Daphne ( daphne project.routing:application) 运行项目时,我得到了错误AppRegistryNotReady: Apps aren't loaded yet

settings.py

INSTALLED_APPS = [
    'channels',
    'whitenoise.runserver_nostatic',
    'django.contrib.staticfiles',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.sites',
    # ...
    # ... installed apps and custom apps
]

WSGI_APPLICATION = 'project.wsgi.application'

ASGI_APPLICATION = 'project.routing.application'

CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {
            "hosts": [REDIS_URL],
        }
    },
}

routing.py

import os

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter

from django.core.asgi import get_asgi_application
from django.conf.urls import url

from my_app.consumers import MyCustomConsumer

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")

application = ProtocolTypeRouter({
    "http": get_asgi_application(),
    'websocket': AuthMiddlewareStack(
        URLRouter([
            url(r'^ws/custom/$', MyCustomConsumer),
        ])
    ),
})

我已经尝试django.setup()按照其他问题中的描述添加,以及使用uvicorn而不是运行,daphne但仍然出现相同的错误。我也尝试过指向 websocket 路由settings.CHANNEL_LAYERS['ROUTING']并将应用程序初始化移出到一个asgi.py文件中,但那里也没有运气。我无法分辨我在做什么与频道文档不同,感谢您的帮助。

4

3 回答 3

9

尽早获取 Django ASGI 应用程序,以确保在导入可能导入 ORM 模型的消费者和 AuthMiddlewareStack 之前填充 AppRegistry。

import os
from django.conf.urls import url
from django.core.asgi import get_asgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
django_asgi_app = get_asgi_application()

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter

from my_app.consumers import MyConsumer

application = ProtocolTypeRouter({
# Django's ASGI application to handle traditional HTTP requests
"http": django_asgi_app,
# WebSocket chat handler
"websocket": AuthMiddlewareStack(
    URLRouter([
        path('ws/custom/', MyConsumer),
    ])
),

})

于 2020-11-25T08:33:09.037 回答
1

上面提到的对我没有任何作用。但我尝试使用以下配置在本地机器上工作:

import os
from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
django_asgi_app = get_asgi_application()

from channels.routing import ProtocolTypeRouter, URLRouter
from app.tokenAuthMiddleware import TokenAuthMiddleware
from app.routing import channel_routing

application = ProtocolTypeRouter({
    "http": django_asgi_app,
    "websocket": TokenAuthMiddleware(
     URLRouter(channel_routing)
    )
})

然后运行以下命令:

gunicorn app.asgi:application -k uvicorn.workers.UvicornH11Worker

使用UvicornH11Worker而不是UvicornWorker为我工作。

您可以查看以下链接以供参考: uvicorn running with gunicorn

于 2021-05-27T14:56:17.227 回答
-1

问题源于将 daphne 服务器指向 中的应用程序routing.py,而它需要指向asgi.py文件。但是,其中settings.pyASGI_APPLICATION需要指向文件中的应用程序routing.py

asgi.py

import os
import django
from channels.routing import get_default_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
django.setup()
application = get_default_application()

设置.py

ASGI_APPLICATION = 'project.routing.application'

路由.py

import os

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.urls import path

from my_app.consumers import MyConsumer


application = ProtocolTypeRouter({
    'websocket': AuthMiddlewareStack(
        URLRouter([
            path('ws/custom/', MyConsumer),
        ])
    ),
})

然后运行 ​​daphne 服务器daphne project.asgi:application

于 2020-10-26T18:19:08.040 回答