[英]How to replace celery task with azure service bus in a django application?
我被要求在 Django 應用程序中使用 azure 服務總線而不是 celery。
閱讀提供的文檔但沒有清楚地了解使用服務總線而不是 celery 任務。 提供的任何建議都會有很大幫助。
在開始之前,我想強調一下 Azure Service Bus 和 Celery 之間的區別。
Azure 服務總線:
Microsoft Azure Service Bus 是一個完全托管的企業集成消息代理。
您可以參考此以了解有關服務總線的更多信息
Celery:
分布式任務隊列。 Celery是一個基於分布式消息傳遞的異步任務隊列/作業隊列。
在您的情況下,我可以想到兩種可能性:
1:您想使用服務總線和 Celery 代替其他消息代理。
你可以參考這個來理解為什么 celery 需要一個消息代理。 我不確定您目前使用的是哪個消息代理,但您可以使用Kombu 庫來滿足您的要求。
Azure服務總線參考: https://docs.celeryproject.org/projects/kombu/en/stable/reference/kombu.transport.azureservicebus.html
其他參考: https://docs.celeryproject.org/projects/kombu/en/stable/reference/index.html
2:用服務總線完全替換Celery以滿足您的要求:
考慮
這是您必須處理的兩個不同的應用程序。
您可以參考下面的內容以獲取更多示例代碼來構建。
https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/servicebus/azure-servicebus/samples
解釋:
以下是接收客戶端的示例:
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 - ########
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.