2015-06-18 139 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') 

即使這將創建一個芹菜任務,就像第一種方法,但這不被推薦。