我按照安装和设置celery的指令过程进行操作,现在我正在尝试执行我的任务。我的项目目录结构如下:
bin
draft1--
|
-------draft1 ----
|
--------celery.py
--------tasks.py
--------views.py
-------manage.py
-------templates
include
lib
这是我的代码:
设置.py
CELERY_BROKER_URL = 'amqp://guest:guest@localhost:5672//'
celery.py
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
app = Celery('app')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
tasks.py
from celery import shared_task
@shared_task
def print_this():
print('ONE MINUTE')
app.views
print_this.delay()
我的celery函数不起作用,它没有执行print语句。我想每分钟执行一次该函数。有什么问题吗?
CELERY_BROKER_URL
对我来说应该与说明中的相同。我尝试在项目根目录(即manage.py所在的位置)中运行./bin/celery -A draft1 worker -l info
,但返回了以下错误:AttributeError: module 'draft1' has no attribute 'app'
/在处理以上异常时,发生了另一个异常:
AttributeError: module 'draft1' has no attribute 'celery'
你知道这可能意味着什么吗?(draft1是我的应用程序名称
) - ZorganCELERY_BROKER_URL ='amqp:// user:password @ host:port'
将您的Django + Celery应用程序指向您的RabbitMQ代理(假设您正在本地主机上运行RabbitMQ服务器)。 - Gokhan Sari