gpt4 book ai didi

python - 从主机运行 supervisord,从 virtualenv(Django 应用程序)运行 celery

转载 作者:IT王子 更新时间:2023-10-29 06:03:53 25 4
gpt4 key购买 nike

我正在尝试使用 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 检查,我会在 command 上收到 fatal error ,我试图在 celery.conf 中运行。帮助!

附注请注意,用户 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'.

最佳答案

celery 二进制文件的路径是 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 app 替换了 -A 参数,而不是主管配置。此 celery 应用程序与您在 celery.conf 中设置的项目目录相关

directory = /home/mhb11/somefolder/myproject

旁注,如果您将 Celery 与 Django 一起使用,则可以使用 Django 的 manage.py 管理 celery,无需直接调用 celery。喜欢

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

有关详细信息,请阅读 Django Celery 的介绍 here .

关于python - 从主机运行 supervisord,从 virtualenv(Django 应用程序)运行 celery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37363393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com