【问题标题】:apscheduler calling function params only onceapscheduler 只调用一次函数参数
【发布时间】:2019-01-29 03:48:01
【问题描述】:

我已经调整了这个基本示例来说明主题中的要点: 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,谢谢。

【问题讨论】:

    标签: python-3.x apscheduler


    【解决方案1】:

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

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

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

    【讨论】:

    • 知道了。谢谢你的解释!
    猜你喜欢
    • 1970-01-01
    • 2022-01-27
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 2012-08-23
    • 1970-01-01
    相关资源
    最近更新 更多