【发布时间】:2015-08-26 01:20:46
【问题描述】:
# In tasks.py file
from __future__ import absolute_import
from celery import shared_task
@shared_task
def randadd(x):
y = randint(0,9)
return x + y
# In views.py
context = {
'add': tasks.randadd(5)
}
def home(request):
global context
return render(request, "home.html", context)
# In home.html
<h1>{{ add }}</h1>
在 home.html 中,我看到 5 +(一个随机整数)的结果。如何重新计算 add 的结果,使其值每 30 分钟更新一次?
我能够找到这个来源:http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html,但我仍然对如何使用它感到困惑。
【问题讨论】:
-
你离这里很远。首先,与其从 randadd 返回值,不如将其存储在某个数据存储中。然后,您的视图应加载此值以显示它。 Beat 是 Celery 内置的定期触发任务的工具。
-
我明白了,你能告诉我如何或建议一个链接吗?