我看了Apscheduler runs once then throws TypeError,但在我的案例中没有用。
我有一个应用程序可以从sqlite数据库中提取日期和时间以及电话号码。使用apscheduler我想让它创建一个sms消息,以便在那个时间出去。现在,无论发生什么错误,它都会触发文本。
from apscheduler.schedulers.background import BackgroundScheduler
scheduler = BackgroundScheduler()
def send_sms(x):
client.messages.create(from_='18595543343',
to="1"+str(x),
body='This is a scheduled sms test.')
def build_sms():
working_data = Customer.query.all()
for each in working_data:
#Check if text has been sent
status = each.verified
if status == 'no':
send_to = each.phone
verified_update = each.verified
verified_update = 'yes'
db.session.commit()
#Get info to build date string
send_date = each.order_date
send_date = str(send_date)
send_time = each.order_time
send_dt_string = send_date + ' ' + send_time + ":00"
#add this info to job queue
scheduler.add_job(send_sms(), 'date', run_date=send_dt_string)
elif status == 'yes':
print("Verified status confirmed")
else:
print('Unknown error')
if __name__ == '__main__':
build_sms()
scheduler.start()
app.run(debug=True)
回溯:
Traceback (most recent call last):
File "C:/Users/Corey/PycharmProjects/core_x/app.py", line 246, in <module>
build_sms()
File "C:/Users/Corey/PycharmProjects/core_x/app.py", line 44, in build_sms
scheduler.add_job(send_sms(x), 'date', run_date=send_dt_string)
File "C:\Users\Corey\AppData\Local\Programs\Python\Python36-32\lib\site-packages\apscheduler\schedulers\base.py", line 427, in add_job
job = Job(self, **job_kwargs)
File "C:\Users\Corey\AppData\Local\Programs\Python\Python36-32\lib\site-packages\apscheduler\job.py", line 44, in __init__
self._modify(id=id or uuid4().hex, **kwargs)
File "C:\Users\Corey\AppData\Local\Programs\Python\Python36-32\lib\site-packages\apscheduler\job.py", line 165, in _modify
raise TypeError('func must be a callable or a textual reference to one')
TypeError: func must be a callable or a textual reference to one
为什么要这样做?
答案 0 :(得分:0)
因为您正在调用send_sms()
,然后将其返回值传递给scheduler.add_job()
,而不是函数本身。