这些天我通过开发我自己的Reddit克隆(在ubuntu 14.04 LTS上)学习Python(2.7)/ Django(1.5)。我试图将Celery(3.1)与Redis合并,使用它定期运行排名算法作为任务(在我的本地设置上)。但遗憾的是,即使只执行一次,我也无法完成这项简单的任务!你能帮我看看我做错了什么吗?
这是目录结构:
-unconnectedreddit (manage.py is here)
-links (tasks.py, models.py, views.py, admin.py)
-unconnectedreddit (celery.py, __init.py___, settings.py, urls.py)
-static
-templates
Celery.py :
from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'unconnectedreddit.settings')
app = Celery('unconnectedreddit', broker='redis://localhost:6379/0', backend='redis://localhost:6379/0',include=['unconnectedreddit.links.tasks'])
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
app.conf.update(
CELERY_TASK_RESULT_EXPIRES=3600,
)
if __name__ == '__main__':
app.start()
settings.py 的添加如下。请注意,添加' djcelery'之后我确实运行了migrate
安装应用程序:
INSTALLED_APPS = ('djcelery',)
import djcelery
djcelery.setup_loader()
BROKER_URL = 'redis://localhost:6379/0'
CELERY_IMPORTS = ('links.tasks', )
CELERY_ALWAYS_EAGER = False
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_IGNORE_RESULT=True
from datetime import timedelta
CELERYBEAT_SCHEDULE = {
'rank_all-every-30-seconds': {
'task': 'tasks.rank_all',
'schedule': timedelta(seconds=30),
},
}
CELERY_TIMEZONE = 'UTC'
__初始化__吡啶:
from __future__ import absolute_import
from .celery import app as celery_app1
tasks.py :
import os
from unconnectedreddit import celery_app1
import time
from links.models import Link
@celery_app1.task
def rank_all():
for link in Link.with_votes.all():
link.set_rank() #ranks interesting 'links' submitted by users
我在终端上运行此命令以启动工作人员:celery -A unconnectedreddit worker -l info
我得到的输出如下:
-------------- celery@has-VirtualBox v3.1.18 (Cipater)
---- **** -----
--- * *** * -- Linux-3.16.0-30-generic-x86_64-with-Ubuntu-14.04-trusty
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app: unconnectedreddit:0x7f938b838910
- ** ---------- .> transport: redis://localhost:6379/0
- ** ---------- .> results: redis://localhost:6379/0
- *** --- * --- .> concurrency: 1 (prefork)
-- ******* ----
--- ***** ----- [queues]
-------------- .> celery exchange=celery(direct) key=celery
[tasks]
. links.tasks.rank_all
[2015-06-04 12:01:17,083: INFO/MainProcess] Connected to redis://localhost:6379/0
[2015-06-04 12:01:17,098: INFO/MainProcess] mingle: searching for neighbors
[2015-06-04 12:01:18,107: INFO/MainProcess] mingle: all alone
/home/has/.virtualenvs/unconnectedreddit/local/lib/python2.7/site-packages/celery/fixups/django.py:265: UserWarning: Using settings.DEBUG leads to a memory leak, never use this setting in production environments!
warnings.warn('Using settings.DEBUG leads to a memory leak, never '
[2015-06-04 12:01:18,136: WARNING/MainProcess] /home/has/.virtualenvs/unconnectedreddit/local/lib/python2.7/site-packages/celery/fixups/django.py:265: UserWarning: Using settings.DEBUG leads to a memory leak, never use this setting in production environments!
warnings.warn('Using settings.DEBUG leads to a memory leak, never '
[2015-06-04 12:01:18,137: WARNING/MainProcess] celery@has-VirtualBox ready.
那就是它。我每30秒就开始定期运行这个任务(参见我的CELERYBEAT_SCHEDULE)。但是我的代码并没有导致它执行一次 - 我的reddit克隆上的排名根本没有变化。任何专家都可以指出我在这个设置中缺少的东西吗?
答案 0 :(得分:0)
错误的任务名称。通过将任务装饰器更改为@celery_app1.task(name='tasks.rank_all')
并调整节拍时间表以包含正确的名称来解决此问题:
CELERYBEAT_SCHEDULE = {
'tasks.rank_all': {
'task': 'tasks.rank_all',
'schedule': timedelta(seconds=30),
},
}