python - Heroku Python仅处理第一个异常

标签 python heroku flask error-handling twilio

目的

根据从csv文件读取的数据发送自定义文本消息。仅当收件人是新收件人或以前成功发送给收件人的邮件时,才发送文本消息。如果csv文件中的电话号码由于任何原因无效,则程序应仅跳至下一个号码。

问题

部署到Heroku时,只有第一个异常可以正确处理。处理第二个无效的电话号码后,不发送任何消息。但是,如果我从终端本地运行程序,它似乎可以正常工作。

编码

def send_sms(num, msg):
    # sends message using Twilio's REST API
    message = client.messages.create(
        to=num, 
        from_=number,
        body=msg,
        status_callback=url)

def prep_msg(file):
    # iterate through csv file containing phone numbers
    for row in file:
        msg = 'test message'
        num = row[7]
        # look for recipient in database of previously sent messages
        record = Status.query.filter_by(to=num).first()
        if record == None or record.status == 'delivered':
            try:
                send_sms(num, msg)
            except Exception:
                continue
    return render_template('success.html')

最佳答案

对此的答案是使用Heroku的 worker dynos异步发送消息作为后台任务。
我使用以下资源对此进行了研究:
https://devcenter.heroku.com/articles/python-rq
http://python-rq.org/docs/

关于python - Heroku Python仅处理第一个异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45417176/

相关文章:

python - 使用BeautifulSoup时,html在某些搜索结果中需要不同索引号的数据

python - 除下一行的值并在数据框中创建列

python - 如何使用 BeautifulSoup 登录亚马逊

python - 如何 : Flask Use Global Logger

python - 在 Flask 上将 Pandas DataFrame 渲染为 html

Matlab 区间数的 Python 模拟,如 1 :2:5

node.js - 部署到heroku时express-rate-limit不起作用

javascript - 将 ember.js 应用程序部署到 Heroku

Heroku Dyno ...在没有工作人员的情况下运行计划任务

flask - 如何在支持的 SQL alchemy 中存储参数,这些参数将传递给 Flask 应用程序中的 celery 任务