【问题标题】:django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. when i try to import models in consumers.py on heroku [duplicate]django.core.exceptions.AppRegistryNotReady:应用程序尚未加载。当我尝试在heroku上的consumers.py中导入模型时[重复]
【发布时间】:2021-12-20 02:24:35
【问题描述】:

我在本地运行我的 django 项目,该项目在 consumer.py 上导入模型,但当我部署时 heroku 发生错误

2021-11-06T10:22:14.039525+00:00 app[web.1]: Traceback (most recent call last):
2021-11-06T10:22:14.039542+00:00 app[web.1]:   File "/app/.heroku/python/bin/daphne", line 8, in <module>
2021-11-06T10:22:14.039607+00:00 app[web.1]:     sys.exit(CommandLineInterface.entrypoint())
2021-11-06T10:22:14.039607+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/daphne/cli.py", line 170, in entrypoint
2021-11-06T10:22:14.039690+00:00 app[web.1]:     cls().run(sys.argv[1:])
2021-11-06T10:22:14.039697+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/daphne/cli.py", line 232, in run
2021-11-06T10:22:14.039772+00:00 app[web.1]:     application = import_by_path(args.application)
2021-11-06T10:22:14.039778+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/daphne/utils.py", line 12, in import_by_path
2021-11-06T10:22:14.039824+00:00 app[web.1]:     target = importlib.import_module(module_path)
2021-11-06T10:22:14.039830+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/importlib/__init__.py", line 127, in import_module
2021-11-06T10:22:14.039892+00:00 app[web.1]:     return _bootstrap._gcd_import(name[level:], package, level)
2021-11-06T10:22:14.039898+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
2021-11-06T10:22:14.039963+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
2021-11-06T10:22:14.040002+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
2021-11-06T10:22:14.040040+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
2021-11-06T10:22:14.040078+00:00 app[web.1]:   File "<frozen importlib._bootstrap_external>", line 850, in exec_module
2021-11-06T10:22:14.040124+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
2021-11-06T10:22:14.040163+00:00 app[web.1]:   File "/app/./spotgame/asgi.py", line 16, in <module>
2021-11-06T10:22:14.040212+00:00 app[web.1]:     import spotifymusicgame.routing
2021-11-06T10:22:14.040218+00:00 app[web.1]:   File "/app/./spotifymusicgame/routing.py", line 3, in <module>
2021-11-06T10:22:14.040261+00:00 app[web.1]:     from . import consumers
2021-11-06T10:22:14.040268+00:00 app[web.1]:   File "/app/./spotifymusicgame/consumers.py", line 59, in <module>
2021-11-06T10:22:14.040324+00:00 app[web.1]:     from .models import roomInfo
2021-11-06T10:22:14.040330+00:00 app[web.1]:   File "/app/./spotifymusicgame/models.py", line 10, in <module>
2021-11-06T10:22:14.040373+00:00 app[web.1]:     class songModel(models.Model):
2021-11-06T10:22:14.040379+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/base.py", line 108, in __new__
2021-11-06T10:22:14.040441+00:00 app[web.1]:     app_config = apps.get_containing_app_config(module)
2021-11-06T10:22:14.040448+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/apps/registry.py", line 253, in get_containing_app_config
2021-11-06T10:22:14.040534+00:00 app[web.1]:     self.check_apps_ready()
2021-11-06T10:22:14.040540+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/django/apps/registry.py", line 136, in check_apps_ready
2021-11-06T10:22:14.040599+00:00 app[web.1]:     raise AppRegistryNotReady("Apps aren't loaded yet.")
2021-11-06T10:22:14.040617+00:00 app[web.1]: django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

这是我的 consumers.py

import json
from channels.generic.websocket import AsyncWebsocketConsumer
from channels.db import database_sync_to_async
from .models import roomInfo
class ChatConsumer(AsyncWebsocketConsumer):
    async def connect(self):
        self.room_name = self.scope['url_route']['kwargs']['room_name']
        self.room_group_name = 'chat_%s' % self.room_name

        # Join room group
        await self.channel_layer.group_add(
            self.room_group_name,
            self.channel_name
        )

        await self.accept()

    async def disconnect(self, close_code):
        # Leave room group
        await self.channel_layer.group_discard(
            self.room_group_name,
            self.channel_name
        )

    # Receive message from WebSocket
    async def receive(self, text_data):
        text_data_json = json.loads(text_data)
        message = text_data_json['message']
        username = text_data_json['username']
        print(username)

        # Send message to room group
        await self.channel_layer.group_send(
            self.room_group_name,
            {
                'type': 'chat_message',
                'message': message,
                'username': username,
            }
        )

    # Receive message from room group
    async def chat_message(self, event):
        message = event['message']
        username = event['username']
        max_user = await self.max_users()
        print(max_user)

        # Send message to WebSocket
        await self.send(text_data=json.dumps({
            'message': message,
            'username': username,
        }))
    
    @database_sync_to_async
    def max_users(self):
        max_user = roomInfo.objects.get(id=1).max_player
        return max_user

asgi.py

import os
import django

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
import spotifymusicgame.routing

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'spotgame.settings')
django.setup()
application = ProtocolTypeRouter({
  "http": get_asgi_application(),
  "websocket": AuthMiddlewareStack(
        URLRouter(
            spotifymusicgame.routing.websocket_urlpatterns
        )
    ),
})

wsgi.py

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'spotgame.settings')

application = get_wsgi_application()

settings.py


ASGI_APPLICATION = "spotgame.routing.application"

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [os.environ.get('REDIS_URL', 'redis://localhost:6379')],
        },
    },
}

过程文件

release: python manage.py migrate
web: daphne spotgame.asgi:application --port $PORT --bind 0.0.0.0 -v2
worker: python manage.py runworker channels --settings=spotgame.settings -v2

我假设 heroku 无法及时加载模型。 如果不清楚我在问什么或是否需要显示任何其他文件,请告诉我。

【问题讨论】:

  • @aaron 是的,这个帖子回答了我的问题

标签: python django heroku django-channels daphne


【解决方案1】:

我现在在 asgi.py 上发现问题 daphne 找不到 Django INSTALLED_APPS 所以 我设置了 DJANGO_SETTINGS_MODULE 工作


import os
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'spotgame.settings')
django.setup()

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
import spotifymusicgame.routing

application = ProtocolTypeRouter({
  "http": get_asgi_application(),
  "websocket": AuthMiddlewareStack(
        URLRouter(
            spotifymusicgame.routing.websocket_urlpatterns
        )
    ),
})

这是我在settings.py中的INSTALLED_APPS


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'corsheaders',
    'django_extensions',
    'channels',
    'spotifymusicgame',
    'users',

]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    • 2016-09-26
    • 2016-01-08
    • 2016-08-22
    相关资源
    最近更新 更多