如果使用Gunicorn运行Django,“ NoneType”对象没有属性“ autodiscover_tasks”

时间:2019-01-21 15:00:26

标签: django celery gunicorn celerybeat

我有一个docker-compose文件,如下所示:

backend:
    image: ${API_IMAGE}
    env_file:
      - .env
    depends_on:
      - db
    volumes:
      - ./data/server/static/:/usr/src/api/static
      - ./data/server/uploads/:/usr/src/api/uploads
    restart: always
frontend:
    image: ${FRONTEND_IMAGE}
    env_file:
      - .env
    ports:
    - "80:80"
    volumes:
    - ./data/server/logs:/usr/src/web/logs
    - ./configs/nginx:/etc/nginx/conf.d
    - ./data/server/static/:/usr/src/app/static
    - ./data/server/uploads/:/usr/src/app/uploads
    restart: always
celery:
    image: ${API_IMAGE}
    env_file:
      - .env
    working_dir: /usr/src/api/
    environment:
      DJANGO_SETTINGS_MODULE: 'core.settings'
    depends_on:
      - redis
    command: /bin/sh -c "celery -E -A core worker -l info"

  beat:
    image: ${API_IMAGE}
    env_file:
      - .env
    working_dir: /usr/src/api/
    environment:
      DJANGO_SETTINGS_MODULE: 'core.settings'
    depends_on:
      - celery
    command: /bin/sh -c "celery -A core beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler"

所有内容都通过frontend容器访问,该容器是Nginx应用程序,使用代理将请求发送到其他容器。

后端正在使用bash文件中的CMD运行:

/usr/local/bin/gunicorn --access-logfile - -w 4 core.wsgi:application -b 0.0.0.0:80 &

一切都很好,并且运行正常。但是,如果我访问管理站点,并尝试使用Celery Beat创建定期任务,请向我显示此错误:

Traceback (most recent call last):
backend_1  |   File "/usr/local/lib/python3.6/site-packages/kombu/utils/objects.py", line 42, in __get__
backend_1  |     return obj.__dict__[self.__name__]
backend_1  | KeyError: '_modules'
backend_1  | 
backend_1  | During handling of the above exception, another exception occurred:

...

File "/usr/local/lib/python3.6/site-packages/celery/app/base.py", line 684, in <listcomp>
backend_1  |     for pkg in fixup.autodiscover_tasks()
backend_1  | AttributeError: 'NoneType' object has no attribute 'autodiscover_tasks'

但是,如果我使用以下命令运行backend

python manage.py runserver 80

没问题,也不例外。一切都很好!

当我带着枪色跑时,尝试从Celery Beat访问“定期任务”时显示错误。

1 个答案:

答案 0 :(得分:0)

我意识到,当我运行python manage.py runserver时,manage.py中有一行设置DJANGO_SETTINGS_MODULE的行,但是如果我使用gunicorn进行运行,则该环境变量不会填充芹菜。 / p>

我在backend容器中设置了环境,一切正常!

.
.
.
backend:
  environment:
    DJANGO_SETTINGS_MODULE: 'core.settings'
.
.
.