用户发起request,并且要等待response返回。但是在视图中有一些耗时的操作,导致用户可能会等待很长时间才能接受response,这样用户体验很差
网站每隔一段时间要同步一次数据,但是http请求是需要触发的
celery网址:http://docs.jinkan.org/docs/celery/
INSTALLED_APPS 添加
djcelery
在settings下方添加如下代码
<span class="hljs-keyword">import</span> djcelery
djcelery.setup_loader()<span class="hljs-comment">#初始化</span>
BROKER_URL=<span class="hljs-string">'redis://:密码@127.0.0.1:6379/0'</span><span class="hljs-comment">#0带表16个库中使用第0个库</span>
CELERY_IMPORTS=(<span class="hljs-string">'App.task'</span>) <span class="hljs-comment">#myApp是项目名</span>
路径 App/task.py
<span class="hljs-keyword">from</span> celery <span class="hljs-keyword">import</span> task
<span class="hljs-keyword">import</span> time
<span class="hljs-meta">@task</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">tt</span><span class="hljs-params">()</span>:</span>
print(<span class="hljs-string">"lucky is a good man"</span>)
time.sleep(<span class="hljs-number">5</span>)
print(<span class="hljs-string">"lucky is a nice man"</span>)
python manage.py migrate
<span class="hljs-keyword">from</span> __future__ <span class="hljs-keyword">import</span> absolute_import
<span class="hljs-keyword">import</span> os
<span class="hljs-keyword">from</span> celery <span class="hljs-keyword">import</span> Celery
<span class="hljs-keyword">from</span> django.conf <span class="hljs-keyword">import</span> settings
os.environ.setdefault(<span class="hljs-string">'DJANGO_SETTINGS_MODULE'</span>, <span class="hljs-string">'whthas_home.settings'</span>)
app = Celery(<span class="hljs-string">'portal'</span>)
app.config_from_object(<span class="hljs-string">'django.conf:settings'</span>)
app.autodiscover_tasks(<span class="hljs-keyword">lambda</span>: settings.INSTALLED_APPS)
<span class="hljs-meta">@app.task(bind=True)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">debug_task</span><span class="hljs-params">(self)</span>:</span>
print(<span class="hljs-string">'Request: {0!r}'</span>.format(self.request))
from .celery import app as celery_app
<span class="hljs-keyword">from</span> .task <span class="hljs-keyword">import</span> tt
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">celery</span><span class="hljs-params">(request)</span>:</span>
tt.delay() <span class="hljs-comment"># 添加到celery中执行,不会阻塞 </span>
<span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">"测试celery"</span>)
传参: 可以在tt.delay([参数]) delay中添加参数
报错信息
File <span class="hljs-string">"/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/celery/utils/timer2.py"</span>, line <span class="hljs-number">19</span>
<span class="hljs-keyword">from</span> kombu.<span class="hljs-keyword">async</span>.timer <span class="hljs-keyword">import</span> Entry, Timer <span class="hljs-keyword">as</span> Schedule, to_timestamp, logger
^
原因:python3.7版本中async是关键字,所以报错
解决方法有两种:
定时执行一个任务
在settings.py文件中添加如下代码
<span class="hljs-keyword">from</span> datetime <span class="hljs-keyword">import</span> timedelta
<span class="hljs-comment"># 在settings.py文件添加</span>
CELERYBEAT_SCHEDULE = {
<span class="hljs-string">'schedule-test'</span>: {
<span class="hljs-string">'task'</span>: <span class="hljs-string">'App.task.test'</span>, <span class="hljs-comment">#App的名称 task为你创建任务的py文件名 test为你的任务的名称</span>
<span class="hljs-string">'schedule'</span>: timedelta(seconds=<span class="hljs-number">3</span>),
<span class="hljs-string">'args'</span>: (<span class="hljs-number">2</span>,)
},
}
启动顺序:
定时多个任务
settings.py添加如下代码
<span class="hljs-keyword">from</span> datetime <span class="hljs-keyword">import</span> timedelta
<span class="hljs-comment"># 在settings.py文件添加</span>
CELERYBEAT_SCHEDULE = {
<span class="hljs-comment">#第一个任务</span>
<span class="hljs-string">'schedule-test1'</span>: {
<span class="hljs-string">'task'</span>: <span class="hljs-string">'App.mytask.test'</span>,
<span class="hljs-string">'schedule'</span>: timedelta(seconds=<span class="hljs-number">3</span>),
<span class="hljs-string">'args'</span>: (<span class="hljs-number">2</span>,)
},
<span class="hljs-comment">#第二个任务</span>
<span class="hljs-string">'schedule-test2'</span>: {
<span class="hljs-string">'task'</span>: <span class="hljs-string">'App.mytask.test2'</span>,
<span class="hljs-string">'schedule'</span>: timedelta(seconds=<span class="hljs-number">3</span>),
},
}
App/task.py
<span class="hljs-keyword">from</span> celery <span class="hljs-keyword">import</span> task
<span class="hljs-keyword">import</span> time
<span class="hljs-meta">@task</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test1</span><span class="hljs-params">(i)</span>:</span>
print(<span class="hljs-string">'打印'</span>,i)
time.sleep(<span class="hljs-number">5</span>)
print(<span class="hljs-string">'打印'</span>,i)
<span class="hljs-meta">@task</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test2</span><span class="hljs-params">()</span>:</span>
print(<span class="hljs-string">'test2'</span>)
time.sleep(<span class="hljs-number">5</span>)
print(<span class="hljs-string">'test2'</span>)