[英]How to continue using my web app as twilio function is running in the background?
我正在開發一個警報應用程序,使用 Django 和 Twilio api 來撥打電話和發送消息。 我發送了將近 2000 個電話和消息,使用 20 個不同的電話號碼,使用類似於下面的 function:
def call_number(phone_numbers, message_to_broadcast):
# index twilio controls the phone number that the message is being sent from
response = VoiceResponse()
response.say('Hello. this is a test message ' + message_to_broadcast + 'Goodbye.', voice='alice' )
index_twilio= 0
try:
for phones in phone_numbers:
client.calls.create(to=phones,
from_=numbers['twilio_numbers'][index_twilio],
status_callback='https://mywebsite.com//ealert/voicedistribution/',
twiml=response)
index_twilio = (0 if index_twilio >= 19 else index_twilio+1)
except (TwilioRestException):
# A generic 400 or 500 level exception from the Twilio API
continue
當我點擊提交按鈕時,我的應用程序一直在加載。 我想立即被重定向到我的主頁,並讓這個 function 在后台運行。
我的問題是:如何恢復使用我的應用程序並立即重定向,而服務器仍在后台進行調用和發送消息? 一直在環顧四周,但我找不到足夠的東西。
另一個問題是,是否可以加快速度而不是以這種方式運行我的兩個函數:
def main():
call_number(phones, message)
text_number(phones, message)
main()
任何批評和/或幫助將不勝感激!
實現此目的的一種方法是實現隊列。 不要在 function 調用中調用或發送消息,而是將它們添加到隊列中,重定向到主頁,然后開始在后台處理隊列。 為此,您可以使用django-celery 。 您可以使用其他一些隊列處理來代替 celery。
另一個簡單的解決方案是使用django-after-response 。 只需向您的 function 添加一個簡單的裝飾器,它將在向用戶發送響應或在您的情況下重定向到主頁后處理代碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.