属性错误:尝试使用Celery和Redis运行Containerized Flask应用程序时,“NoneType”对象没有属性“Redis”

ktca8awb  于 2023-10-15  发布在  Redis
关注(0)|答案(1)|浏览(240)

我正试图运行一个Flask应用程序沿着与celery ,以便在后台运行一些进程。我想用集装箱的方式来做。我已经尝试了许多不同的配置,但我不能让celery 工人运行。任何提示,解决方案,想法,任何东西都将被高度赞赏。
我使用mambaorg/micromamba:latest作为应用程序映像的基础映像,在其中安装以下内容(使用environment.yml):

name: base
channels:
  - conda-forge
  - anaconda
dependencies:
  - python=3.10.6
  - mne=1.1.1
  - mne-features=0.2.1
  - ray-default
  - flask
  - celery
  - redis

我的docker-compose文件看起来像这样:

version: '2.4'

services:
  redis-service:
    image: redis:latest
    container_name: redis-service

  celery-service:
    image: myapp:1.0.0
    container_name: celery-service
    environment:
      - FLASK_APP=run
    volumes:
      - .:/app/
    depends_on:
      - redis-service
    command: celery -A run.celery worker --loglevel=info

  genfeatsbs:
    image: myapp:1.0.0
    container_name: myapp
    environment:
      - FLASK_APP=run
      - FLASK_DEBUG=1
    volumes:
      - .:/app/ # All my source code and my python initialization script 'run.py'
    ports:
      - '5000:5000'
    depends_on:
      - redis-service
    command: flask run -h 0.0.0.0

我的应用程序的**init.py**看起来像:

from celery import Celery, Task
from flask import Flask

from app.routes.execution_blueprint import execution_blueprint

def make_celery(app: Flask) -> Celery:
    class FlaskTask(Task):
        def __call__(self, *args: object, **kwargs: object) -> object:
            with app.app_context():
                return self.run(*args, **kwargs)
        
    celery = Celery(app.name)
    celery.conf.update(app.config["CELERY"])
    celery.Task = FlaskTask
    
    return celery

def create_app(config_file) -> Flask:
    app = Flask(__name__)
    app.config.from_object(config_file)
    
    celery = make_celery(app)
    celery.set_default()
    
    app.register_blueprint(execution_blueprint)
    
    return app, celery

我的celery 配置看起来像:

CELERY = {
        'broker_url': 'redis://redis-service',
        'result_backend': 'redis://redis-service'
    }

我还尝试添加端口(6379)和索引(0)。
我的脚本启动我的应用程序和celery 工作器是:

from myapp import create_app

app, celery = create_app('app.config.Config')
app.app_context().push()

不知道发生了什么,但无论我做什么,我都无法让celery 工人运行,完整的错误是:

Traceback (most recent call last):
  File "/opt/conda/bin/celery", line 10, in <module>
    sys.exit(main())
  File "/opt/conda/lib/python3.10/site-packages/celery/__main__.py", line 15, in main
    sys.exit(_main())
  File "/opt/conda/lib/python3.10/site-packages/celery/bin/celery.py", line 235, in main
    return celery(auto_envvar_prefix="CELERY")
  File "/opt/conda/lib/python3.10/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
  File "/opt/conda/lib/python3.10/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
  File "/opt/conda/lib/python3.10/site-packages/click/core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/opt/conda/lib/python3.10/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/opt/conda/lib/python3.10/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
  File "/opt/conda/lib/python3.10/site-packages/click/decorators.py", line 33, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/opt/conda/lib/python3.10/site-packages/celery/bin/base.py", line 134, in caller
    return f(ctx, *args, **kwargs)
  File "/opt/conda/lib/python3.10/site-packages/celery/bin/worker.py", line 348, in worker
    worker = app.Worker(
  File "/opt/conda/lib/python3.10/site-packages/celery/worker/worker.py", line 98, in __init__
    self.setup_instance(**self.prepare_args(**kwargs))
  File "/opt/conda/lib/python3.10/site-packages/celery/worker/worker.py", line 119, in setup_instance
    self._conninfo = self.app.connection_for_read()
  File "/opt/conda/lib/python3.10/site-packages/celery/app/base.py", line 818, in connection_for_read
    return self._connection(url or self.conf.broker_read_url, **kwargs)
  File "/opt/conda/lib/python3.10/site-packages/celery/app/base.py", line 877, in _connection
    return self.amqp.Connection(
  File "/opt/conda/lib/python3.10/site-packages/kombu/connection.py", line 201, in __init__
    if not get_transport_cls(transport).can_parse_url:
  File "/opt/conda/lib/python3.10/site-packages/kombu/transport/__init__.py", line 90, in get_transport_cls
    _transport_cache[transport] = resolve_transport(transport)
  File "/opt/conda/lib/python3.10/site-packages/kombu/transport/__init__.py", line 75, in resolve_transport
    return symbol_by_name(transport)
  File "/opt/conda/lib/python3.10/site-packages/kombu/utils/imports.py", line 59, in symbol_by_name
    module = imp(module_name, package=package, **kwargs)
  File "/opt/conda/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 "/opt/conda/lib/python3.10/site-packages/kombu/transport/redis.py", line 266, in <module>
    class PrefixedStrictRedis(GlobalKeyPrefixMixin, redis.Redis):
AttributeError: 'NoneType' object has no attribute 'Redis'
t30tvxxf

t30tvxxf1#

请在yaml文件中设置Redis URI

redis://redis:6379/0

另外,连接字符串应该具有以下格式来连接到Redis

redis://[USER:PASSWORD@]REDIS_ADDRESS[:PORT][/VIRTUALHOST]
rediss://[USER:PASSWORD@]REDIS_ADDRESS[:PORT][/VIRTUALHOST]

你可以参考下面的例子

version: "3.7"
services:
  web:
    build:
      context: ./api
      dockerfile: Dockerfile
    restart: always
    ports:
     - "5001:5001"
    depends_on:
      - redis
    volumes: ['./api:/api']
  worker:
    build:
      context: ./celery-queue
      dockerfile: Dockerfile
    command: celery -A tasks worker -l info -E
    environment:
      CELERY_BROKER_URL: redis://redis
      CELERY_RESULT_BACKEND: redis://redis
    depends_on:
      - redis
    volumes: ['./celery-queue:/queue']
  monitor:
    build:
      context: ./celery-queue
      dockerfile: Dockerfile
    ports:
     - "5555:5555"
    command:  ['celery', 'flower', '-A', 'tasks']
    environment:
      CELERY_BROKER_URL: redis://redis:6379/0
      CELERY_RESULT_BACKEND: redis://redis:6379/0
    depends_on:
      - redis
      - worker
    volumes: ['./celery-queue:/queue']
  redis:
    image: redis:alpine
    ports:
      - "6379:6379"

相关问题