亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python分布式系統(tǒng)Celery安裝使用實(shí)例講解

 更新時(shí)間:2023年12月26日 10:54:15   作者:簡(jiǎn)訊Alfred  
這篇文章主要為大家介紹了python分布式系統(tǒng)Celery安裝使用實(shí)例講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

Celery 使用

Celery 是一個(gè)簡(jiǎn)單高效可靠的分布式系統(tǒng)。在處理大量消息,實(shí)時(shí)處理異步任務(wù),定時(shí)執(zhí)行任務(wù),支持任務(wù)調(diào)度等方面使用起來(lái)更為靈活。

簡(jiǎn)單理解 Celery 就是發(fā)布任務(wù)(Producer),消息中間件(Broker)接收任務(wù),執(zhí)行任務(wù)(Worker)或定時(shí)執(zhí)行任務(wù)(Celery Beat)。

任務(wù)隊(duì)列

任務(wù)隊(duì)列,顧名思義就是任務(wù)工作單元的集合,集合中存放了等待執(zhí)行的任務(wù)。

何時(shí)使用任務(wù)隊(duì)列?假如當(dāng)前我們需要執(zhí)行某項(xiàng)任務(wù),但是該任務(wù)比較耗時(shí),且可以放在后臺(tái)執(zhí)行,為了不影響當(dāng)前操作,我們就把該任務(wù)放在任務(wù)隊(duì)列,等待調(diào)用和執(zhí)行。最常使用的場(chǎng)景是在 web 開(kāi)發(fā)中。例如我們需要實(shí)現(xiàn)一個(gè)用戶(hù)認(rèn)證發(fā)送電子郵件的功能,如果不使用異步執(zhí)行,我們需要等待電子郵件發(fā)送完成才可執(zhí)行下步操作,但是發(fā)送郵件的過(guò)程中,web 頁(yè)面會(huì)處于假死狀態(tài),影響用戶(hù)使用體驗(yàn)。簡(jiǎn)單的 web 應(yīng)用可以用多線(xiàn)程來(lái)完成該任務(wù),一旦任務(wù)量加重,還是需要使用更為強(qiáng)大的 Celery。

定時(shí)執(zhí)行任務(wù),可以理解為 crontab。

安裝

使用 Celery 的第一步就是選擇消息中間件。這里我們選擇 redis,上手快好操作。

安裝 redis

官方文檔[1]有詳細(xì)的源碼安裝說(shuō)明。

這里介紹在 Ubuntu 16.04 下通過(guò) apt 工具安裝:

apt-get install redis-server -y

啟動(dòng) redis:

redis-server

測(cè)試是否成功安裝:

?  ~ redis-cli
127.0.0.1:6379> PING
PONG

說(shuō)明安裝成功。

終端輸入:

python3 -m pip install redis

這樣我們可以使用 python 操作 redis。

安裝 Celery

終端執(zhí)行:

python3 -m pip install celery

這樣就完成了 redis 和 Celery 的安裝。

簡(jiǎn)單應(yīng)用

當(dāng)我們安裝好 redis,并啟動(dòng)服務(wù)后,可以創(chuàng)建一個(gè)新的文件夾用來(lái)練習(xí) celery。

mkdir celery_test

進(jìn)入該目錄后,創(chuàng)建文件 test.py,輸入以下內(nèi)容:

from celery import Celery
app = Celery(
    'hello',
    broker="redis://127.0.0.1:6379/1"
)
@app.task
def hello():
    # 耗時(shí)需要異步執(zhí)行的任務(wù)
    return 'hello world'

這就完成了 Celery 的簡(jiǎn)單應(yīng)用。

如何使用呢?

我們需要在終端啟動(dòng) Celery 服務(wù)。輸入:

celery worker -A test --loglevel INFO

使用 python shell 簡(jiǎn)單試一下:

>>> from test import hello
>>> hello.delay()
<AsyncResult: 182f57d7-71fc-4c61-b6b4-dbdd76ac4068>

是不是很簡(jiǎn)單?這樣就完成了 Celery 的簡(jiǎn)單使用。

使用配置文件

我們可以將 Celery 任務(wù)單獨(dú)做成模塊的形式,并通過(guò)配置文件來(lái)配置 Celery。

結(jié)構(gòu)如下:

.
├── app
│   ├── __init__.py
│   ├── config.py
│   └── tasks.py
└── test.py

app 文件夾下存放 Celery 相關(guān)配置和任務(wù)。

__init__.py 用來(lái)初始化 Celery,config.py 是 Celery 的配置文件,tasks.py 存放任務(wù)。

test.py 用來(lái)測(cè)試執(zhí)行任務(wù)。

init.py

from celery import Celery
app = Celery("hello")
# 使用配置文件
app.config_from_object("app.config")

config.py

"""
Celery 配置
"""
broker_url = "redis://127.0.0.1:6379/1"

很多教程基本使用都是大寫(xiě)配置,但官方文檔中介紹在 4.0 以后開(kāi)始推薦使用小寫(xiě)配置,所以依據(jù)文檔這里使用小寫(xiě)配置。

具體更多詳細(xì)配置可以查看Configuration and defaults[2]。

tasks.py

from app import app
@app.task
def hello():
    return "hello, world!"

這個(gè)文件存放耗時(shí)任務(wù)。

在終端啟動(dòng) Celery:

celery worker -A app.tasks --loglevel INFO

啟動(dòng)成功后在 python shell 中測(cè)試:

>>> from app import tasks
>>> tasks.hello.delay()
<AsyncResult: 3aaa9e64-66ea-42dc-bed7-b1921275e9f9>

查看 Celery,和之前一樣,說(shuō)明就執(zhí)行成功啦!

部署 Celery 和 redis

我們需要使用 supervisor 啟動(dòng)和管理 Celery 和 redis 服務(wù)。

pip install supervisor

安裝完成后修改配置文件 /etc/supervisord.conf,加入以下配置(redis 的配置要寫(xiě)在 Celery 之前):

[program:redis]
user = root
directory=/root/celery_test
command=redis-server
numprocs=1
startsecs = 5
startretries = 3
autostart=true
autorestart=true
; stdout 日志文件,需要注意當(dāng)指定目錄不存在時(shí)無(wú)法正常啟動(dòng),所以需要手動(dòng)創(chuàng)建目錄(supervisord 會(huì)自動(dòng)創(chuàng)建日志文件)
stdout_logfile = /root/logs/redis_stdout.log

[program:celery_worker]
user = root
directory=/root/celery_test
command=celery worker -A app.tasks --loglevel INFO --logfile /root/celery_worker.log
numprocs=1
startsecs = 5
startretries = 3
autostart=true
autorestart=true
stdout_logfile = /root/logs/celery_stdout.log

輸入命令啟動(dòng)服務(wù):

supervisord -c /etc/supervisord.conf

這樣就完成了 redis 和 Celery 的部署。

引用鏈接

官方文檔: https://redis.io/download#installation 

Configuration and defaults: http://docs.celeryproject.org/en/latest/userguide/configuration.html#new-lowercase-settings 

以上就是python分布式系統(tǒng)Celery安裝使用實(shí)例講解的詳細(xì)內(nèi)容,更多關(guān)于python分布式系統(tǒng)Celery的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論