芹菜中的简单定期任务不起作用但没有错误

时间:2016-10-26 14:04:50

标签: python django celery django-celery

我是Celery的新手。我正在尝试使用我的Django项目正确配置Celery。为了测试芹菜是否有效,我创建了一个周期性任务,应该每2秒打印一次“periodic_task”。不幸的是它不起作用但没有错误。

1 已安装的rabbitmq

2 html2canvas(document.getElementById("talltweets"), { onrendered: function(canvas) { var screenshot = canvas.toDataURL("image/png"); document.getElementById("textScreenshot").setAttribute("src", screenshot); } }); function edValueKeyPress() { var edValue = document.getElementById("body"); var s = edValue.value; var lblValue = document.getElementById("talltweets"); lblValue.innerText = s; }

<script src="http://files.codepedia.info/uploads/iScripts/html2canvas.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>



<input type="text" onKeyPress="edValueKeyPress()" onKeyUp="edValueKeyPress()" id="body">


<div id="talltweets"></div>
<!-- The PNG image will be added here-->
<div>
  <img id="textScreenshot" />
</div>

3 Project/project/celery.py

from __future__ import absolute_import

import os

from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')

from django.conf import settings  # noqa

app = Celery('project')

app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

@app.task(bind=True)
def myfunc():
    print 'periodic_task'

@app.task(bind=True)
def debudeg_task(self):
    print('Request: {0!r}'.format(self.request))

4 Project/project/__init__.py

from __future__ import absolute_import

from .celery import app as celery_app 

Settings.py之前,我运行INSTALLED_APPS = [ 'djcelery', ...] ... ... CELERYBEAT_SCHEDULE = { 'schedule-name': { 'task': 'project.celery.myfunc', # We are going to create a email_sending_method later in this post. 'schedule': timedelta(seconds=2), }, }

仍然看不到每2秒在控制台中打印出任何“periodic_task”......你知道该怎么办吗?

编辑CELLERY CONSOLE:

python manage.py

修改

在改变工人之后,它似乎有效。每2秒发生一次事情(改为5秒),但我看不到任务的结果。 (我可以把任何东西放到CELERYBEAT_SCHEDULE中,甚至是错误的路径,它不会引起任何错误..)

我将myfunc代码更改为:

celery -A project worker -l info

但我无法在任何地方看到text.txt。

 -------------- celery@Milwou_NB v3.1.23 (Cipater)
---- **** -----
--- * ***  * -- Windows-8-6.2.9200
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app:         dolava:0x33d1350
- ** ---------- .> transport:   amqp://guest:**@localhost:5672//
- ** ---------- .> results:     disabled://
- *** --- * --- .> concurrency: 4 (prefork)
-- ******* ----
--- ***** ----- [queues]
 -------------- .> celery           exchange=celery(direct) key=celery


[tasks]
  . project.celery.debudeg_task
  . project.celery.myfunc

1 个答案:

答案 0 :(得分:3)

尝试运行

$ celery -A project beat -l info