Debian 9.8
virtualenv 16.4.3
Python 3.5.3
Django 2.1.7
芹菜4.3.0
django-celery-beat 1.4.0
当我以celery worker -A personnel
或celery beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler
之类的venv活动从应用程序目录运行celery命令时,它会按预期工作。我的应用程序的名称是“人员”。
有几个相关问题,但与我的问题完全不同。当我尝试各种命令时,会发生以下情况。
celery worker
从venv处于活动状态的项目目录中启动工作程序,并按照文档显示的方式进行工作(redis正常工作,beat也正常工作)
/home/www/personnel/venv/bin/python
从venv不活动的proj目录外部进入,将我放入一个shell中,我可以在该shell中导入而不会出错
/home/www/personnel/venv/bin/celery
从项目外部显示给我celery帮助(用法:celery [options] ...)
/home/www/personnel/venv/bin/python /home/www/personnel celery worker
应该起作用,它返回:
/home/www/personnel/venv/bin/python: can't find '__main__' module in '/home/www/personnel'
即使带有-A标志,也会返回上述错误。
/home/www/personnel/venv/bin/python /home/www/personnel/manage.py celery worker
返回:
Unknown command: 'celery'
Type 'manage.py help' for usage.
标准输出日志显示相同的错误。我猜该命令应该在主管开始起作用之前起作用。
我从头开始,并以相同的结果重新创建了venv。
# __init__.py
from __future__ import absolute_import, unicode_literals
from .celery import app as celery_app
__all__ = ['celery_app']
# celery.py
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
#from celery.schedules import crontab
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'personnel.settings')
app = Celery('personnel')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
; ==================================
; celery worker config
; ==================================
[program:celery]
; full path using virtualenv
command=/home/www/personnel/venv/bin/python /home/www/personnel celery worker -A personnel -I info
directory=/home/www/personnel
;user=dunnoyet
numprocs=1
stderr_logfile=/var/log/celery.out.log
stdout_logfile=/var/log/celery.err.log
autostart=true
autorestart=true
startsecs=10
; need to wait for currently executing tasks to finish at shutdown
; increase if long running tasks
stopwaitsecs = 600
; send the termination signal (SIGTERM) to the whole process group
stopasgroup=true
; set Celery priority higher than default (999)
; so, if rabbitmq is supervised, it will start first
priority=1000
# Project structure based on django cookie cutter
pr_project
|--personnel
|--api/
|--...
|--__init__.py
|--celery.py
|--urls.py
|--views.py
|--wsgi.py
|--requirements
|--...
在项目目录中,它可以按预期工作,但我无法使其在外部工作或与主管一起成功启动。
修改
#personnel/settings.py
CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
按照建议,我尝试了/home/www/personnel/venv/bin/celery worker
-它开始了,但是看起来好像无法连接到Redis
[2019-04-05 09:18:11,960: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 111] Connection refused.
带有应用标志/home/www/personnel/venv/bin/celery worker -A personnel
:
Error:
Unable to load celery application.
The module personnel was not found.
答案 0 :(得分:0)
使用超级用户时,它可以与:
[program:celery]
; full path using virtualenv
command=/home/www/personnel celery worker
directory=/home/www/personnel
从命令行尝试时它将无法工作,也许是因为它需要可访问Django的设置。