[英]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.