2015-06-18 141 views
1

我试图关注django的芹菜doc。这里是我的项目结构:芹菜定期任务不工作

├── hiren 
│   ├── celery_app.py 
│   ├── __init__.py 
│   ├── settings.py 
│   ├── urls.py 
│   └── wsgi.py 
├── manage.py 
├── reminder 
│   ├── admin.py 
│   ├── __init__.py 
│   ├── migrations 
│   ├── models.py 
│   ├── serializers.py 
│   |── task.py 
│   |── tests.py 
│ |── views.py 

这里是我的settings.py文件:

BROKER_URL = 'redis://localhost:6379/4' 
CELERYBEAT_SCHEDULE = { 
    'run-every-5-seconds': { 
     'task': 'reminder.task.run', 
     'schedule': timedelta(seconds=5), 
     'args': (16, 16) 
    }, 
} 

和提醒/ task.py文件:

def run(): 
    print('hello') 

当我运行celery -A hiren beat -l debug命令我没在终端看不到“你好”的文字。我错过了什么?

+0

你看过:http://celery.readthedocs.org/en/latest/django/first-steps-with-django.html#using-the-shared-task-decorator? – Brandon

回答

5

要从任何可调用对象创建任务,您需要使用task()修饰器。这将为run()创建一个芹菜任务。

提醒/ task.py:

from celery import Celery 

app = Celery('tasks', broker='redis://localhost') 

@app.task 
def run(): 
    print('hello') 

芹菜库必须在使用前被实例化,这种情况下被称为一个应用程序(或app的简称)。

如果您使用的是“老”模块基于芹菜API,那么你可以导入任务装饰像这样:

from celery import task 

@task 
def run(): 
    print('hello') 

即使这将创建一个芹菜任务,就像第一种方法,但这不被推荐。