亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 django 應用程序中用 azure 服務總線替換 celery 任務?

如何在 django 應用程序中用 azure 服務總線替換 celery 任務?

Cats萌萌 2023-07-11 10:41:13
我被要求在 Django 應用程序中使用 azure 服務總線而不是 celery。閱讀提供的文檔,但沒有清楚地了解使用服務總線而不是 celery 任務。提供的任何建議都會有很大幫助。
查看完整描述

1 回答

?
紅顏莎娜

TA貢獻1842條經驗 獲得超13個贊

在開始討論之前,我想強調一下 Azure 服務總線和 Celery 之間的差異。

Azure服務總線:

Microsoft Azure 服務總線是完全托管的企業集成消息代理。

芹菜 :

分布式任務隊列。Celery是一個基于分布式消息傳遞的異步任務隊列/作業隊列。

對于你的情況我可以想到兩種可能性:

  1. 您希望將 Service Bus 與 Celery 一起使用來代替其他消息代理。

  2. 用服務總線替換 Celery

1:您希望將 Service Bus 與 Celery 一起使用來代替其他消息代理。


2:完全用Service Bus替換Celery?以滿足您的要求:

考慮

  • 消息發送者是生產者

  • 消息接收者是消費者

這是您必須處理的兩個不同的應用程序。

解釋 :

  • 每次您想要執行操作時,您都可以從生產者客戶端向主題發送消息。

  • 消費者客戶端 - 正在偵聽的應用程序將接收消息并處理該消息。您可以將自定義流程附加到它 - 這樣,只要消費者客戶端收到消息,您的自定義流程就會執行。

以下是接收客戶端的示例:

from azure.servicebus.aio import SubscriptionClient

import asyncio

import nest_asyncio

nest_asyncio.apply()


? ? ? ??

Receiving = True


#Topic 1 receiver :?

conn_str= "<>"

name="Allmessages1"

SubsClient = SubscriptionClient.from_connection_string(conn_str, name)

receiver =? SubsClient.get_receiver()


async def receive_message_from1():

? ? await receiver.open()

? ? print("Opening the Receiver for Topic1")

? ? async with receiver:

? ? ? while(Receiving):

? ? ? ? msgs =? await receiver.fetch_next()

? ? ? ? for m in msgs:

? ? ? ? ? ? print("Received the message from topic 1.....")

? ? ? ? ? ? ##### - Your code to execute when a message is received - ########

? ? ? ? ? ? print(str(m))

? ? ? ? ? ? ##### - Your code to execute when a message is received - ########

? ? ? ? ? ? await m.complete()

? ? ? ? ? ??

? ? ? ? ? ??

loop = asyncio.get_event_loop()

topic1receiver = loop.create_task(receive_message_from1())

下面一行之間的部分是每次收到消息時將執行的指令。


##### - Your code to execute when a message is received - ########


查看完整回答
反對 回復 2023-07-11
  • 1 回答
  • 0 關注
  • 132 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號