我正在尝试让我的django项目在后台运行定期任务,这就是我所拥有的。
我的celery.py
:
from __future__ import absolute_import
import os
from datetime import timedelta
import django
from celery import Celery
from celery.schedules import crontab
from django.conf import settings
CELERY_TIMEZONE = 'UTC'
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'linker.settings')
django.setup()
app = Celery('linker',
broker='amqp://',
backend='amqp://',
)
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
CELERYBEAT_SCHEDULE = {
'add-every-30-seconds': {
'task': 'distributors.tasks.add',
'schedule': timedelta(seconds=30),
'args': (16, 16)
},
}
tasks.py
:
from __future__ import absolute_import
from celery import shared_task
@shared_task
def add(x, y):
print(x + y)
@shared_task
def mul(x, y):
return x * y
@shared_task
def xsum(numbers):
return sum(numbers)
@shared_task
def update_ds(pk):
pass
当我尝试运行celery -A linker worker -B -l info
时,我看到标准的芹菜:
-------------- celery@ovd v3.1.18 (Cipater)
---- **** -----
--- * *** * -- Linux-3.13.0-55-generic-x86_64-with-Ubuntu-14.04-trusty
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app: linker:0x7f84c73df940
- ** ---------- .> transport: amqp://guest:**@localhost:5672//
- ** ---------- .> results: amqp://
- *** --- * --- .> concurrency: 4 (prefork)
-- ******* ----
--- ***** ----- [queues]
-------------- .> celery exchange=celery(direct) key=celery
但我没有看到任何结果或至少发生过程。
使这项工作成功并将打印信息启用到控制台的方法是什么?