溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Django中怎么使用Celery實(shí)現(xiàn)異步

發(fā)布時(shí)間:2021-06-16 16:39:28 來(lái)源:億速云 閱讀:156 作者:Leah 欄目:開發(fā)技術(shù)

本篇文章為大家展示了Django中怎么使用Celery實(shí)現(xiàn)異步,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

apt-get install python3
pip3 install django

嗯,就是兩條命令的事兒。

再說celery的安裝:

pip3 install celery
pip3 install redis==2.10.6

目前奶牛所在的時(shí)間redis for python的版本是redis-3.0.1,為什么要用2.10.6呢?因?yàn)?.0.1壓根配置就無(wú)法運(yùn)行?。?!

繼續(xù)安裝redis server

apt-get install redis
service redis start

然后就可以按照celery的官方教程走了,放個(gè)URL:http://docs.celeryproject.org/en/latest/django/index.html

python3 manage.py startproject nenew
cd nenew
python3 manage.py startapp nenewapp
touch ./nenew/celery.py
touch ./nenewapp/tasks.py

然后增加nenew/nenew/celery.py內(nèi)容為

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'nenew.settings')

app = Celery('nenew')

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#  should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()


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

增加nenew/nenew/__init__.py的內(nèi)容

from __future__ import absolute_import, unicode_literals

# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app

__all__ = ('celery_app',)

增加nenew/nenewtest/tasks.py的內(nèi)容

# Create your tasks here
from __future__ import absolute_import, unicode_literals
from celery import shared_task


@shared_task
def add(x, y):
  return x + y


@shared_task
def mul(x, y):
  return x * y


@shared_task
def xsum(numbers):
  return sum(numbers)

在nenew/nenew/settings.py中增加和修改

...

ALLOWED_HOSTS = ['*']
....

INSTALLED_APPS = [
...
  'nenewtest',
]

...

CELERY_BROKER_URL = 'redis://localhost:6379/1'
CELERY_RESULT_BACKEND = ‘redis://localhost:6379/0'

在nenew/nenewtest/views.py中增加或修改為

from django.shortcuts import render
from django.http import HttpResponse
from .tasks import add
# Create your views here.
def nenewtest(request):
  result = add.delay('2','2')
  result.ready()
  return HttpResponse('nenew Django Celery worker run !')

最后把views添加到nenew/nenew/urls.py中

from django.contrib import admin
from django.urls import path
from nenewtest import views

urlpatterns = [
  path('admin/', admin.site.urls),
  path('test/', views.nenewtest),
]

然后在項(xiàng)目目錄nenew執(zhí)行

celery -A nenew worker -l info

這時(shí)候worker啟動(dòng)正常會(huì)顯示:

-------------- <a href="/cdn-cgi/l/email-protection" rel="external nofollow" data-cfemail="2c4f4940495e556c424942495b">[email protected]</a> v4.2.1 (windowlicker)
---- **** -----
--- * ***  * -- Linux-4.15.0-39-generic-x86_64-with-Ubuntu-18.04-bionic 2018-11-23 17:31:25
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app:         nenew:0x7fdc5a155cc0
- ** ---------- .> transport:   redis://localhost:6379/1
- ** ---------- .> results:     redis://localhost:6379/0
- *** --- * --- .> concurrency: 1 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery

這樣類似的信息,然后我們啟動(dòng)項(xiàng)目,這里需要新開一個(gè)shell:

python3 manage.py runserver 0:80

這樣我們就可以通過80端口直接訪問我們的web了。地址是http://locahost/test

當(dāng)我們這里訪問正常后,在worker界面會(huì)有

[2018-11-23 18:09:19,469: INFO/MainProcess] Received task: nenewtest.tasks.add[35faa0fe-dd48-4f8d-9559-516556a93a40]
[2018-11-23 18:09:19,470: INFO/ForkPoolWorker-1] Task nenewtest.tasks.add[35faa0fe-dd48-4f8d-9559-516556a93a40] succeeded in 0.00031037399821798317s: '22'

如下語(yǔ)句表示執(zhí)行成功,這樣子就表示通過Django的網(wǎng)頁(yè)我們對(duì)celery任務(wù)的異步執(zhí)行成功。

當(dāng)然,按照我的方法是可以一步成功的,因?yàn)槟膛R呀?jīng)踩了一整天的坑了,被一個(gè)錯(cuò)誤郁悶得不要不要的。

AttributeError: 'float' object has no attribute 'items'

就是這個(gè)錯(cuò)誤,查遍國(guó)內(nèi)的所有網(wǎng)站都沒有結(jié)果,然后就去bing的國(guó)際版查,然后發(fā)現(xiàn)果然是有bug在啊,奶牛這一天浪費(fèi)得可真是夠了。

這是celery的一個(gè)issue,在地址https://github.com/celery/celery/issues/5175 ,issue里面提及在2018/11/22日 it's fixed in kombu and celery master。問題的根源是celery對(duì)redis3的支持不好,補(bǔ)救方法是

Solution: Roll back redis with pip: pip install redis==2.10.6

然后在commit里面找到是對(duì)requirements/extras/redis.txt文件進(jìn)行版本限定。

-redis>=2.10.5
+redis>=2.10.5,<3

上述內(nèi)容就是Django中怎么使用Celery實(shí)現(xiàn)異步,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI