# celery.py
from celery.schedules import crontab
from celery import Celery
celery = Celery('scheduler.celery',
include=['scheduler.tasks'])
celery.config_from_object('celeryconfig')
和
# tasks.py
from scheduler.celery import celery
@celery.task
def test():
do_something()
和
# celeryconfig.py
from celery.schedules import crontab
CELERYBEAT_SCHEDULE = {
'test-cron': {
'task': 'tasks.test',
'schedule': crontab(minute='*/1'),
},
}
# CELERY_IMPORTS = ('tasks', )
BROKER_URL = 'redis://localhost:6379/0'
所有文件都在projects/scheduler/
文件夹下
当我启动celeryd
服务时,我可以看到它正在运行并连接到我的代理,但是当我启动celerybeat
服务时,我可以在日志中看到消息:Received unregistered task of type 'tasks.test'
。
如果我取消注释CELERY_IMPORTS
常量(如SO中的许多答案所示),celeryd
服务甚至都不会启动!实际上,它输出OK
但是使用ps ef | grep celery
我可以看到它没有运行。
我的守护程序配置文件如下所示:
# Name of nodes to start
CELERYD_NODES="w1"
# Where to chdir at start.
CELERYD_CHDIR="/home/me/projects/scheduler/"
# Extra arguments to celeryd
CELERYD_OPTS="--time-limit=300 --concurrency=4"
# %n will be replaced with the nodename.
CELERYD_LOG_FILE="/var/log/celery/%n.log"
CELERYD_PID_FILE="/var/run/celery/%n.pid"
# Workers should run as an unprivileged user.
CELERYD_USER="celery"
CELERYD_GROUP="celery"
# Extra arguments to celerybeat
CELERYBEAT_OPTS="--schedule=/var/run/celerybeat-schedule"
非常感谢任何帮助。
答案 0 :(得分:2)
如果您没有scheduler
模块并从模块根目录运行芹菜:
<强> runner.py
强>:
from celery_test import celery
if __name__ == '__main__':
celery.start()
将 celery.py
重命名为 celery_test.py
:
from celery import Celery
celery = Celery('scheduler.celery',
include=['tasks'])
celery.config_from_object('celeryconfig')
<强> celeryconfig.py
强>:
from celery.schedules import crontab
CELERYBEAT_SCHEDULE = {
'test-cron': {
'task': 'tasks.test',
'schedule': crontab(minute='*/1'),
},
}
BROKER_URL = 'redis://localhost:6379/0'
tasks.py
,修复导入:
from celery_test import celery
@celery.task
def test():
do_something()
您必须小心导入并添加runner.py
,因为它会导入celery_test
,然后导入tasks
,然后再导入celery_test
。
如果您有scheduler
模块并从模块根目录运行芹菜:
__init__.py
为空。
<强> runner.py
强>:
from scheduler.celery_test import celery
if __name__ == '__main__':
celery.start()
将 celery.py
重命名为 celery_test.py
:
from celery import Celery
celery = Celery('scheduler.celery',
include=['scheduler.tasks'])
celery.config_from_object('celeryconfig')
celeryconfig.py
,修复了默认任务名称:
from celery.schedules import crontab
CELERYBEAT_SCHEDULE = {
'test-cron': {
'task': 'scheduler.tasks.test',
'schedule': crontab(minute='*/1'),
},
}
BROKER_URL = 'redis://localhost:6379/0'
<强> tasks.py
强>:
from scheduler.celery_test import celery
@celery.task
def test():
do_something()