如何继续使用我的网络应用程序,因为后台的 twilio 函数是 运行?

How to continue using my web app as twilio function is running in the background?

我正在开发一个警报应用程序,使用 Django 和 Twilio api 来拨打电话和发送消息。我使用 20 个不同的 phone 号码发送了将近 2000 个电话和消息,使用的函数类似于以下函数:

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

当我点击提交按钮时,我的应用程序一直在加载。我想立即重定向到我的主页,并在后台使用此功能 运行。

我的问题是:当服务器仍在后台调用和发送消息时,如何继续使用我的应用程序并立即重定向?一直在四处寻找,但我找不到足够的东西。

另一个问题是,是否有可能使它更快而不是 运行以这种方式连接我的两个函数:


def main():
    call_number(phones, message)
    text_number(phones, message)

main()

任何批评and/or帮助将不胜感激!

实现此目的的一种方法是实施队列。不要在函数调用中调用或发送消息,而是将它们添加到队列中,重定向到主页,然后开始在后台处理队列。为此,您可以使用 django-celery。您可以使用其他一些队列处理来代替芹菜。

另一个简单的解决方案是使用 django-after-response。只需向您的函数添加一个简单的装饰器,它就会在向用户发送响应或在您的情况下重定向到主页后处理代码