apscheduler 只调用函数参数一次

apscheduler calling function params only once

我调整了这个基本示例来说明主题中的要点: https://github.com/agronholm/apscheduler/blob/master/examples/executors/processpool.py

这是调整后的代码(参见 args=[datetime.now()])

#!/usr/bin/env python

from datetime import datetime
import os

from apscheduler.schedulers.blocking import BlockingScheduler


def tick(param):
    print('Tick! The time is: %s' % param)


if __name__ == '__main__':
    scheduler = BlockingScheduler()
    scheduler.add_executor('processpool')
    scheduler.add_job(tick, 'interval', seconds=3, args=[datetime.now()])
    print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))

    try:
        scheduler.start()
    except (KeyboardInterrupt, SystemExit):
        pass

当我运行它时,输出时间戳不更新:

$ ./test.py
Press Ctrl+C to exit
Tick! The time is: 2019-01-28 19:41:53.131599
Tick! The time is: 2019-01-28 19:41:53.131599
Tick! The time is: 2019-01-28 19:41:53.131599

这是预期的行为吗?我正在使用 Python 3.6.7 和 apscheduler 3.5.3,谢谢。

这与APScheduler无关。你正在做的事情可以这样重写:

args = [datetime.now()]
scheduler.add_job(tick, 'interval', seconds=3, args=args)

您正在调用 datetime.now(),然后将列表中的 return 值 传递给 scheduler.add_job()。由于您传递的是日期时间,因此每次执行目标函数时,您如何期望 APScheduler 调用 datetime.now()