APScheduler 触发一次然后出错

APScheduler triggers once then errors

我阅读了 ,但对我的情况没有帮助。

我有一个应用程序可以从 sqlite 数据库中提取日期和时间以及 phone 数字。使用 apscheduler 我希望它创建一条短信,以便在当时的那个日期出去。现在,无论发生什么,它都会触发文本然后引发错误。

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

为什么要这样做?

因为您正在调用 send_sms(),然后将其 return 值 传递给 scheduler.add_job(),而不是函数本身。