Django中使用Celery的方法步驟
(一)、概述
Celery是一個(gè)簡(jiǎn)單、靈活和可靠的基于多任務(wù)的分布式系統(tǒng),為運(yùn)營(yíng)提供用于維護(hù)此系統(tǒng)的工具。專注于實(shí)時(shí)處理的任務(wù)隊(duì)列,同時(shí)也支持任務(wù)的調(diào)度。執(zhí)行單元為任務(wù)(task),利用多線程這些任務(wù)可以被并發(fā)的在單個(gè)或多個(gè)職程(worker)上運(yùn)行。
Celery通過(guò)消息機(jī)制通信,通常通過(guò)中間人(broker)來(lái)分配和調(diào)節(jié)客戶端與職程服務(wù)器(worker)之間的通信??蛻舳税l(fā)送一條消息,中間人把消息分配給一個(gè)職程,最后由職程來(lái)負(fù)責(zé)執(zhí)行此任務(wù)。
Celery可以有多個(gè)職程和中間人,這樣提高了高可用性和橫向的擴(kuò)展能力
Celery由python語(yǔ)言開(kāi)發(fā),但是該協(xié)議可以用任何語(yǔ)言拉力實(shí)現(xiàn),例如:Django中的Celery、node中的node-celery和php中的celery-php
(二)、Django中使用Celery的流程與配置
導(dǎo)入Celery:pip3 install Celery
在 與項(xiàng)目同名的目錄下 創(chuàng)建celery.py文件,特別注意:項(xiàng)目同名的目錄下
復(fù)制內(nèi)容到該文件
修改兩處內(nèi)容
- os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')中的proj改為項(xiàng)目名
- app = Celery('pro')中的pro改為項(xiàng)目名
import os from celery import Celery # set the default Django settings module for the 'celery' program. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings') app = Celery('pro') # 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(f'Request: {self.request!r}')
在 與項(xiàng)目同名的目錄下 的__init__.py文件中添加內(nèi)容
# 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',)
在settings.py文件中添加配置
- CELERY_BROKER_URL:中間人url,可以配置redis或者RabbitMQ
- CELERY_RESULT_BACKEND:返回結(jié)果的存儲(chǔ)地址
- CELERY_ACCEPT_CONTENT:接收內(nèi)容的格式,分為兩種:json和msgpack。msgpack比json格式的數(shù)據(jù)體積更小,傳輸速度更快。
- CELERY_TASK_SERIALIZER:任務(wù)載荷的序列化方式-->json
- CELERY_TIMEZONE
- CELERY_TASK_TRACK_STARTED:是否開(kāi)啟任務(wù)跟蹤
- CELERY_TASK_TIME_LIMIT:任務(wù)超時(shí)限制
# Celery配置 CELERY_BROKER_URL = env("CELERY_BROKER_URL") CELERY_RESULT_BACKEND = env("CELERY_RESULT_BACKEND") CELERY_ACCEPT_CONTENT = ["json", "msgpack"] CELERY_TASK_SERIALIZER = "json" CELERY_TIMEZONE = "Asia/Shanghai" CELERY_TASK_TRACK_STARTED = True CELERY_TASK_TIME_LIMIT = 30 * 60
在app下創(chuàng)建tasks.py文件,創(chuàng)建發(fā)送消息功能,任務(wù)方法必須添加裝飾器:@shared_task
from rest_framework.response import Response from rest_framework.generics import GenericAPIView from time import sleep from celery import shared_task class TestView3(GenericAPIView): @classmethod @shared_task def sleep(self, duration): sleep(duration) return Response("成功", status=200)
創(chuàng)建視圖和路由
### views.py from .tasks import TestView3 class TestView1(GenericAPIView): def get(self, request): TestView3.sleep(10) return Response("celery實(shí)驗(yàn)成功") test_view_1 = TestView1.as_view() ### urls.py from django.urls import path from .views import ( test_view_1 ) urlpatterns = [ path('celery/', test_view_1, name="test1") ]
安裝redis并啟動(dòng)
啟動(dòng)django項(xiàng)目
使用Celery命令啟動(dòng)Celery服務(wù),命令:celery -A 項(xiàng)目名 worker -l info,如果如下所示則為啟動(dòng)成功.
celery@AppledeMacBook-Air.local v5.0.3 (singularity) Darwin-20.1.0-x86_64-i386-64bit 2020-12-05 20:52:17 [config] .> app: drf_email_project:0x7f84a0c4ad68 .> transport: redis://127.0.0.1:6379/1%20 .> results: redis://127.0.0.1:6379/2 .> concurrency: 4 (prefork) .> task events: OFF (enable -E to monitor tasks in this worker) [queues] .> celery exchange=celery(direct) key=celery [tasks] . drf_email_project.celery.debug_task . users.tasks.sleep [2020-12-05 20:52:18,166: INFO/MainProcess] Connected to redis://127.0.0.1:6379/1%20 [2020-12-05 20:52:18,179: INFO/MainProcess] mingle: searching for neighbors [2020-12-05 20:52:19,212: INFO/MainProcess] mingle: all alone [2020-12-05 20:52:19,248: WARNING/MainProcess] /Users/apple/drf-email/lib/python3.7/site-packages/celery/fixups/django.py:204: UserWarning: Using settings.DEBUG leads to a memory leak, never use this setting in production environments! leak, never use this setting in production environments!''') [2020-12-05 20:52:19,249: INFO/MainProces
到此這篇關(guān)于Django中使用Celery的方法步驟的文章就介紹到這了,更多相關(guān)Django使用Celery的方法步驟內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python入門(mén)之三角函數(shù)sin()函數(shù)實(shí)例詳解
這篇文章主要介紹了Python入門(mén)之三角函數(shù)sin()函數(shù)實(shí)例詳解,分享了相關(guān)實(shí)例,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11python自動(dòng)化實(shí)現(xiàn)登錄獲取圖片驗(yàn)證碼功能
這篇文章主要介紹了python自動(dòng)化實(shí)現(xiàn)登錄獲取圖片驗(yàn)證碼功能,本文通過(guò)實(shí)例截圖的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11Pytorch實(shí)現(xiàn)基于CharRNN的文本分類與生成示例
今天小編就為大家分享一篇Pytorch實(shí)現(xiàn)基于CharRNN的文本分類與生成示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01pandas中concatenate和combine_first的用法詳解
本文主要介紹了pandas中concatenate和combine_first的用法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01Windows 下python3.8環(huán)境安裝教程圖文詳解
這篇文章主要介紹了Windows 下python3.8環(huán)境安裝教程圖文詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03python使用多線程不斷刷新網(wǎng)頁(yè)的方法
這篇文章主要介紹了python使用多線程不斷刷新網(wǎng)頁(yè)的方法,涉及Python多線程thread及time模塊操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03Python利用雪花算法實(shí)現(xiàn)生成唯一ID
雪花算法是在一個(gè)項(xiàng)目體系中生成全局唯一ID標(biāo)識(shí)的一種方式,偶然間看到了Python使用雪花算法不盡感嘆真的是太便捷了。本文就來(lái)聊聊這具體的實(shí)現(xiàn)方法,感興趣的可以了解一下2022-11-11