当前位置: 首页 > 知识库问答 >
问题:

芹菜容器在使用django cache_页面时崩溃(InvalidCacheBackendError)

林鸿彩
2023-03-14

我的Django webapp项目面临一个问题。我正在运行一个使用Django、Postgres、Redis和芹菜的集装箱化环境。主要是,我想使用Redis缓存和芹菜设置实时更新。到目前为止,我已经能够连接到redis和芹菜,并将芹菜任务结果存储在redis缓存中。当我试图使用Redis在django中缓存页面时,事情变得一团糟。出于某种原因,使用django的缓存系统(cache\u pagedecorator)会破坏我的芹菜容器。

错误

我的芹菜容器遇到以下错误:

django.core.cache.backends.base.InvalidCacheBackendError:找不到后端django_redis.cache.RedisCache:没有名为django_redis的模块

以下是完整的回溯

Traceback (most recent call last):

File "/usr/local/lib/python3.9/site-packages/django/core/cache/__init__.py", line 50, in _create_cache

backend_cls = import_string(backend)

File "/usr/local/lib/python3.9/site-packages/django/utils/module_loading.py", line 17, in import_string

module = import_module(module_path)

File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module

return _bootstrap._gcd_import(name[level:], package, level)

File "<frozen importlib._bootstrap>", line 1030, in _gcd_import

File "<frozen importlib._bootstrap>", line 1007, in _find_and_load

File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked

File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed

File "<frozen importlib._bootstrap>", line 1030, in _gcd_import

File "<frozen importlib._bootstrap>", line 1007, in _find_and_load

File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked

ModuleNotFoundError: No module named 'django_redis'


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

File "/usr/local/bin/celery", line 8, in <module>

sys.exit(main())

File "/usr/local/lib/python3.9/site-packages/celery/__main__.py", line 15, in main

sys.exit(_main())

File "/usr/local/lib/python3.9/site-packages/celery/bin/celery.py", line 213, in main

return celery(auto_envvar_prefix="CELERY")

File "/usr/local/lib/python3.9/site-packages/click/core.py", line 829, in __call__

return self.main(*args, **kwargs)

File "/usr/local/lib/python3.9/site-packages/click/core.py", line 782, in main

rv = self.invoke(ctx)

File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1259, in invoke

return _process_result(sub_ctx.command.invoke(sub_ctx))

File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1066, in invoke

return ctx.invoke(self.callback, **ctx.params)

File "/usr/local/lib/python3.9/site-packages/click/core.py", line 610, in invoke

return callback(*args, **kwargs)

File "/usr/local/lib/python3.9/site-packages/click/decorators.py", line 21, in new_func

return f(get_current_context(), *args, **kwargs)

File "/usr/local/lib/python3.9/site-packages/celery/bin/base.py", line 132, in caller

return f(ctx, *args, **kwargs)

File "/usr/local/lib/python3.9/site-packages/celery/bin/worker.py", line 320, in worker

worker = app.Worker(

File "/usr/local/lib/python3.9/site-packages/celery/worker/worker.py", line 94, in __init__

self.app.loader.init_worker()

File "/usr/local/lib/python3.9/site-packages/celery/loaders/base.py", line 111, in init_worker

self.import_default_modules()

File "/usr/local/lib/python3.9/site-packages/celery/loaders/base.py", line 105, in import_default_modules

raise response

File "/usr/local/lib/python3.9/site-packages/celery/utils/dispatch/signal.py", line 276, in send

response = receiver(signal=self, sender=sender, **named)

File "/usr/local/lib/python3.9/site-packages/celery/fixups/django.py", line 82, in on_import_modules

self.worker_fixup.validate_models()

File "/usr/local/lib/python3.9/site-packages/celery/fixups/django.py", line 121, in validate_models

run_checks()

File "/usr/local/lib/python3.9/site-packages/django/core/checks/registry.py", line 70, in run_checks

new_errors = check(app_configs=app_configs, databases=databases)

File "/usr/local/lib/python3.9/site-packages/django/core/checks/urls.py", line 13, in check_url_config

return check_resolver(resolver)

File "/usr/local/lib/python3.9/site-packages/django/core/checks/urls.py", line 23, in check_resolver

return check_method()

File "/usr/local/lib/python3.9/site-packages/django/urls/resolvers.py", line 408, in check

for pattern in self.url_patterns:

File "/usr/local/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__

res = instance.__dict__[self.name] = self.func(instance)

File "/usr/local/lib/python3.9/site-packages/django/urls/resolvers.py", line 589, in url_patterns

patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)

File "/usr/local/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__

res = instance.__dict__[self.name] = self.func(instance)

File "/usr/local/lib/python3.9/site-packages/django/urls/resolvers.py", line 582, in urlconf_module

return import_module(self.urlconf_name)

File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module

return _bootstrap._gcd_import(name[level:], package, level)

File "<frozen importlib._bootstrap>", line 1030, in _gcd_import

File "<frozen importlib._bootstrap>", line 1007, in _find_and_load

File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked

File "<frozen importlib._bootstrap>", line 680, in _load_unlocked

File "<frozen importlib._bootstrap_external>", line 790, in exec_module

File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed

File "/code/footballdata/urls.py", line 23, in <module>

from api import views

File "/code/api/views.py", line 21, in <module>

from .mixins import CacheMixin

File "/code/api/mixins.py", line 10, in <module>

class CacheMixin(object):

File "/code/api/mixins.py", line 16, in CacheMixin

def list(self, request):

File "/usr/local/lib/python3.9/site-packages/django/utils/decorators.py", line 63, in _dec

return _multi_decorate(decorator, obj)

File "/usr/local/lib/python3.9/site-packages/django/utils/decorators.py", line 47, in _multi_decorate

_update_method_wrapper(_wrapper, dec)

File "/usr/local/lib/python3.9/site-packages/django/utils/decorators.py", line 17, in _update_method_wrapper

def dummy(*args, **kwargs):

File "/usr/local/lib/python3.9/site-packages/django/utils/decorators.py", line 117, in _decorator

middleware = middleware_class(view_func, *m_args, **m_kwargs)

File "/usr/local/lib/python3.9/site-packages/django/middleware/cache.py", line 174, in __init__

super().__init__(get_response)

File "/usr/local/lib/python3.9/site-packages/django/middleware/cache.py", line 67, in __init__

super().__init__(get_response)

File "/usr/local/lib/python3.9/site-packages/django/middleware/cache.py", line 133, in __init__

self.cache = caches[self.cache_alias]

File "/usr/local/lib/python3.9/site-packages/django/core/cache/__init__.py", line 79, in __getitem__

cache = _create_cache(alias)

File "/usr/local/lib/python3.9/site-packages/django/core/cache/__init__.py", line 52, in _create_cache

raise InvalidCacheBackendError(

django.core.cache.backends.base.InvalidCacheBackendError: Could not find backend 'django_redis.cache.RedisCache': No module named 'django_redis'

安装程序

将Django与Django redis一起使用==4.12。1和redis==3.5。3.使用docker compose进行停靠。

我的docker-compose.yml

version: "3.9"
   
services:
  postgresdb:
    image: postgres
    container_name: postgresdb
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    volumes: 
      - pgdata:/code/pgdata/

  redisdb:
    image: redis
    container_name: redisdb
    volumes: 
      - redisdata:/code/redisdata/

  web:
    build: .
    container_name: django
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    environment: 
      - CELERY_BROKER=redis://redisdb:6379/0
      - CELERY_BACKEND=redis://redisdb:6379/0
    ports:
      - "8000:8000"
    depends_on:
      - postgresdb
      - redisdb

  celery:
    build: .
    container_name: celery
    command: celery -A footballdata worker -l INFO
    volumes: 
      - .:/code
    depends_on: 
      - web
      - redisdb

volumes: 
  pgdata:
  redisdata:

在我的设置中。py

        CACHES = {
        "default": {
            "BACKEND": "django_redis.cache.RedisCache",
            "LOCATION": "redis://redisdb:6379/",
            "OPTIONS": {
                "CLIENT_CLASS": "django_redis.client.DefaultClient",
            }
        }
    }

CACHE_TTL = 30

# Celery stuff 
CELERY_BROKER_URL = os.environ.get("CELERY_BROKER", "redis://redisdb:6379/0")
CELERY_RESULT_BACKEND = os.environ.get("CELERY_BROKER", "redis://redisdb:6379/0")
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Europe/Warsaw'

# Redis 
REDIS_HOST = 'redisdb'
REDIS_PORT = 6379

是什么导致容器崩溃

使用django的cache\u页面decorator。我有一个RESTAPI,我想连接到缓存。我使用简单的mixin缓存页面:

CACHE_TTL = getattr(settings, 'CACHE_TTL', DEFAULT_TIMEOUT)

class CacheMixin(object):
    """
    Caching for REST API views.
    Caches list and retrieve results of the view for the specified TTL.
    """
    @method_decorator(cache_page(CACHE_TTL))
    def list(self, request):
        return super().list(request)

    @method_decorator(cache_page(CACHE_TTL))
    def retrieve(self, request, pk):
        return super().retrieve(request, pk)

用这个调味品弄坏了我的芹菜容器。当我注释掉这段代码时,容器运行得很好。

据我所见,服务器试图创建或连接到我在settings.py中指定的缓存后端,但没有找到django_redis模块。这很奇怪,因为我已经安装了它,并在我的requirements.txt文件中。

更奇怪的是(至少对我来说)其他容器工作得很好——缓存mixin也工作得很好!当我连接到redis容器并用这个mixin缓存页面时,它正确地存储在缓存中。只是芹菜因为某种原因坏了。

我试过的

>

  • 环顾四周,寻找类似的问题。我找到的最接近的是这个问题。但是,这个问题与six包有关。我的追踪是不同的。除此之外,我还没有找到任何对我有帮助的东西。

    恢复到较旧版本的django redis(3.6.6)和/或redis(2.10)-我发现过去有些人与django和redis存在兼容性问题。这对我也不起作用。

    有没有人有过类似的问题?如果有任何想法,我将不胜感激。

  • 共有1个答案

    岳英耀
    2023-03-14

    好吧,我解决了。结果证明它比我想象的要简单(通常是这样)。我应该听一下错误信息,知道它来自哪里。

    发生的事情是,我在需求文件中做了一些更改,但我没有使用docker compose-up--build重建容器,而是使用docker build重建容器 。因此,我猜芹菜容器没有正确更新所需的包装。

    所以这个错误本质上是因为我对docker vs docker-comment缺乏理解。

     类似资料:
    • 我使用getStringArrayListExtra()搜索将列表从一个活动传输到另一个活动。这在第一次(从MainActivity到Diag2Activity)时运行良好,但在第二次(从Diag2Activity到SSToActivity)时应用程序崩溃。 每次我使用相同的方法:第一次活动: 第二项活动: 谁能告诉我错误可能来自哪里?提前谢谢 请在下面找到完整的代码: 主要活动。JAVA } 主

    • 现在,我想将< code>register事件发布到某个特殊的交换,我可以使用celery远程检索和处理它。 实际上,我已经使用了函数来实现这一点,但是它必须传递来指示应该执行哪个任务并消费它。所以它似乎不太适合我的目标。 我想要的就是这样: 向某些发布消息; 远程机器1订阅此或并捕获消息,用于执行任务; 远程机器2-与机器1相同但执行另一个任务-接收(可能需要回复某些) 例如,就像这个工作流一样

    • 我正在使用mariadb和wordpress容器。但是这个错误一直在发生。我如何确保这种崩溃不再发生?我被攻击了吗?或者是发生在其他人身上的问题?我怎样才能连接到mariadb,并访问shell,并试图找出mariadb容器中发生了什么? 请看下面每次崩溃后记录的消息...页面点击量似乎也很高。页面访问量高达20,000到60,000次点击。这些似乎是爬虫,机器人的工作。不确定这些是不是恶意攻击。

    • 每当UwAmp重启自己(例如,当我修改php.ini或切换到不同版本的php),它经常崩溃Chrome我打开的任何页面,无论是我正在UwAmp测试的页面还是完全不相关的东西比如Facebook它们是空的,我必须重新加载它们。有时它完全关闭Chrome,我必须重新启动它,我收到消息说Chrome意外关闭。什么给?这是已知的错误吗?有什么办法可以预防吗?UwAmp是如何控制Chrome的?这是在Win

    • 问题内容: 我想每5分钟检查一次服务中的实际时间,然后根据时间将手机静音或取消静音。早些时候,我尝试在线程.sleep(300000)的末尾使用while(true),但是它总是与ANR一起崩溃,所以我尝试使用计时器任务,但是现在它在带有NullPointerException 的行之后崩溃 LogCat: 问题答案: 您需要在onCreate内初始化audioManager。

    • 我有一个非常奇怪的问题。 我所设置的是一个文件,它向雅虎财经网站发送一个url请求,然后使用结果在JFrame中绘制一个JFreeChart。 我就是不明白以下几点: 对于某些url请求,JFrame崩溃 它会启动,但只显示一个白色屏幕。而对于其他请求,我的程序运行良好。 例如: 本请求: 很好。 但是这个要求: 导致错误。 这怎么可能? 我知道以下几点: JFrame崩溃,但JVM没有注意到它(