【问题标题】:Running supervisord from the host, celery from a virtualenv (Django app)从主机运行 supervisord,从 virtualenv(Django 应用程序)运行 celery
【发布时间】:2016-05-21 13:10:24
【问题描述】:

我正在尝试使用 celery 和 redis 队列为我的 Django 应用程序执行任务。 Supervisord 通过apt-get 安装在主机上,而 celery 位于我系统上的特定 virtualenv 中,通过 `pip.

因此,我似乎无法通过 supervisord 运行 celery 命令。如果我从 virtualenv 内部运行它,它可以正常工作,在它之外,它不会。如何让它在我当前的设置下运行?解决方案是否只是通过 apt-get 安装celery,而不是在 virtualenv 中?请指教。

我在 /etc/supervisor/conf.d 里面的 celery.conf 是:

[program:celery]
command=/home/mhb11/.virtualenvs/myenv/local/lib/python2.7/site-packages/celery/bin/celery -A /etc/supervisor/conf.d/celery.conf -l info
directory = /home/mhb11/somefolder/myproject
environment=PATH="/home/mhb11/.virtualenvs/myenv/bin",VIRTUAL_ENV="/home/mhb11/.virtualenvs/myenv",PYTHONPATH="/home/mhb11/.virtualenvs/myenv/lib/python2.7:/home/mhb11/.virtualenvs/myenv/lib/python2.7/site-packages"
user=mhb11
numprocs=1
stdout_logfile = /etc/supervisor/logs/celery-worker.log
stderr_logfile = /etc/supervisor/logs/celery-worker.log
autostart = true
autorestart = true
startsecs=10

stopwaitsecs = 600

killasgroup = true

priority = 998

我的 Django 项目的文件夹结构是:

/home/mhb11/somefolder/myproject
├── myproject
│   ├── celery.py       # The Celery app file
│   ├── __init__.py     # The project module file (modified)
│   ├── settings.py     # Including Celery settings
│   ├── urls.py
│   └── wsgi.py
├── manage.py
├── celerybeat-schedule
└── myapp
    ├── __init__.py
    ├── models.py
    ├── tasks.py        # File containing tasks for this app
    ├── tests.py
    └── views.py

如果我通过 supervisorctl 进行status 检查,我会在尝试在 celery.conf 中运行的command 上收到致命错误。救命!

附言请注意,用户 mhb11 没有 root 权限,以防万一。此外,/etc/supervisor/logs/celery-worker.log 是空的。在supervisord.log 内部,我看到的相关错误是INFO spawnerr: can't find command '/home/mhb11/.virtualenvs/redditpk/local/lib/python2.7/site-packages/celery/bin/‌​celery'

【问题讨论】:

  • 从终端运行/home/mhb11/.virtualenvs/myenv/local/lib/python2.7/site-packages/celery/bin/celery -A /etc/supervisor/conf.d/celery.conf -l info 会发生什么?
  • /etc/supervisor/logs/celery-worker.log 有什么? /var/log/supervisord.log/var/log/supervisor/supervisord.log 有什么作用?
  • @MuhammadTahir:/etc/supervisor/logs/celery-worker.log 为空。在supervisord.log 内部,我看到的相关错误是INFO spawnerr: can't find command '/home/mhb11/.virtualenvs/redditpk/local/lib/python2.7/site-packages/celery/bin/celery'
  • @MuhammadTahir:如果我尝试运行/home/mhb11/.virtualenvs/myenv/local/lib/python2.7/site-packages/celery/bin/cel‌​ery -A /etc/supervisor/conf.d/celery.conf -l info,我会得到-bash: /home/mhb11/.virtualenvs/myenv/local/lib/python2.7/site-packages/celery/bin/cel‌​ery: No such file or directory

标签: python django redis celery supervisord


【解决方案1】:

芹菜二进制文件的路径是myenv/bin/celery,而您使用的是myenv/local/lib/python2.7/site-packages/celery/bin/cel‌‌​​ery

因此,如果您在终端上尝试传递给主管的命令 (command=xxx),您应该会收到相同的错误。

您需要将 celery.conf 中的 command=xxx 替换为

command=/home/mhb11/.virtualenvs/myenv/bin/celery -A myproject.celery -l info

请注意,我还用 celery 应用程序替换了 -A 参数,而不是主管配置。这个 celery 应用程序与您在celery.conf 中设置的项目目录相关

directory = /home/mhb11/somefolder/myproject

附带说明,如果您在 Django 中使用 Celery,您可以使用 Django 的 manage.py 管理 celery,无需直接调用 celery。喜欢

python manage.py celery worker
python manage.py celery beat

详情请阅读 Django Celery 简介here

【讨论】:

  • 塔希尔,感谢您的回复。我会试一试,然后回复你。
  • 你是对的。我忽略了这部分文档,因为我认为我需要访问 celery 的安装文件夹。而我实际上需要访问/.virtualenvs/myenv/bin/celery,即保存可执行脚本的文件夹。
  • Django Celery 现在已经过时了 - '旧的 django celery 集成项目。'
猜你喜欢
  • 1970-01-01
  • 2020-02-24
  • 1970-01-01
  • 2022-11-18
  • 1970-01-01
  • 2016-09-16
  • 1970-01-01
  • 2014-12-26
  • 2019-09-30
相关资源
最近更新 更多