目的
根据从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/