Python 哪种组合触发器导致apscheduler中的作业执行?

Python 哪种组合触发器导致apscheduler中的作业执行?,python,apscheduler,Python,Apscheduler,TL;DR:在APScheduler中时,是否可以知道是哪个原始触发器导致作业执行 长话短说:我正在尝试运行通过SNMP从设备获取数据的任务。有些任务每2分钟运行一次,有些每5分钟运行一次,有些每10分钟运行一次。。。但是,当间隔对齐时,出于性能原因,将这些任务合并到单个查询是很重要的 解决这个问题的一种方法是实现一个定制,但这似乎有点复杂。将任务组合到一个作业中似乎更容易,该作业可以在任何时间间隔上运行-但是,我需要知道哪个时间间隔触发了作业执行。有没有办法得到这些信息 小例子: from a

TL;DR:在APScheduler中时,是否可以知道是哪个原始触发器导致作业执行

长话短说:我正在尝试运行通过SNMP从设备获取数据的任务。有些任务每2分钟运行一次,有些每5分钟运行一次,有些每10分钟运行一次。。。但是,当间隔对齐时,出于性能原因,将这些任务合并到单个查询是很重要的

解决这个问题的一种方法是实现一个定制,但这似乎有点复杂。将任务组合到一个作业中似乎更容易,该作业可以在任何时间间隔上运行-但是,我需要知道哪个时间间隔触发了作业执行。有没有办法得到这些信息

小例子:

from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.triggers.combining import OrTrigger
from apscheduler.triggers.interval import IntervalTrigger
import logging


logging.basicConfig(format='%(asctime)s | %(levelname)s | %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S', level=logging.DEBUG)
log = logging.getLogger("{}.{}".format(__name__, "base"))


def test_func(*args, **kwargs):
    log.warn("Which interval(s) caused this job to execute? I only got args: {}, kwargs: {}".format(args, kwargs))


if __name__ == "__main__":
    scheduler = BlockingScheduler()

    # apply config to scheduler:
    intervals = [10, 15, 50]
    trigger = OrTrigger([IntervalTrigger(seconds=sec) for sec in intervals])
    scheduler.add_job(test_func, trigger=trigger, kwargs={"a": 123, "b": 345})

    try:
        scheduler.start()
    except KeyboardInterrupt:
        log.info("Got exit signal, exiting.")
输出:

2019-09-08 09:57:59 | INFO | Adding job tentatively -- it will be properly scheduled when the scheduler starts
2019-09-08 09:57:59 | INFO | Added job "test_func" to job store "default"
2019-09-08 09:57:59 | INFO | Scheduler started
2019-09-08 09:57:59 | DEBUG | Looking for jobs to run
2019-09-08 09:57:59 | DEBUG | Next wakeup is due at 2019-09-08 09:58:09.453275+02:00 (in 9.998746 seconds)

2019-09-08 09:58:09 | DEBUG | Looking for jobs to run
2019-09-08 09:58:09 | INFO | Running job "test_func (trigger: or[interval[0:00:10], interval[0:00:15], interval[0:00:50]], next run at: 2019-09-08 09:58:09 CEST)" (scheduled at 2019-09-08 09:58:09.453275+02:00)
2019-09-08 09:58:09 | WARNING | Which interval(s) caused this job to execute? I only got args: (), kwargs: {'a': 123, 'b': 345}
2019-09-08 09:58:09 | INFO | Job "test_func (trigger: or[interval[0:00:10], interval[0:00:15], interval[0:00:50]], next run at: 2019-09-08 09:58:09 CEST)" executed successfully
2019-09-08 09:58:09 | DEBUG | Next wakeup is due at 2019-09-08 09:58:19.453275+02:00 (in 9.999404 seconds)

2019-09-08 09:58:19 | DEBUG | Looking for jobs to run
2019-09-08 09:58:19 | INFO | Running job "test_func (trigger: or[interval[0:00:10], interval[0:00:15], interval[0:00:50]], next run at: 2019-09-08 09:58:19 CEST)" (scheduled at 2019-09-08 09:58:19.453275+02:00)
2019-09-08 09:58:19 | WARNING | Which interval(s) caused this job to execute? I only got args: (), kwargs: {'a': 123, 'b': 345}
2019-09-08 09:58:19 | INFO | Job "test_func (trigger: or[interval[0:00:10], interval[0:00:15], interval[0:00:50]], next run at: 2019-09-08 09:58:19 CEST)" executed successfully
2019-09-08 09:58:19 | DEBUG | Next wakeup is due at 2019-09-08 09:58:29.453275+02:00 (in 9.997439 seconds)

2019-09-08 09:58:29 | DEBUG | Looking for jobs to run
2019-09-08 09:58:29 | INFO | Running job "test_func (trigger: or[interval[0:00:10], interval[0:00:15], interval[0:00:50]], next run at: 2019-09-08 09:58:29 CEST)" (scheduled at 2019-09-08 09:58:29.453275+02:00)
2019-09-08 09:58:29 | WARNING | Which interval(s) caused this job to execute? I only got args: (), kwargs: {'a': 123, 'b': 345}
2019-09-08 09:58:29 | INFO | Job "test_func (trigger: or[interval[0:00:10], interval[0:00:15], interval[0:00:50]], next run at: 2019-09-08 09:58:29 CEST)" executed successfully
2019-09-08 09:58:29 | DEBUG | Next wakeup is due at 2019-09-08 09:58:39.453275+02:00 (in 9.997883 seconds)

2019-09-08 09:58:39 | DEBUG | Looking for jobs to run
2019-09-08 09:58:39 | INFO | Running job "test_func (trigger: or[interval[0:00:10], interval[0:00:15], interval[0:00:50]], next run at: 2019-09-08 09:58:39 CEST)" (scheduled at 2019-09-08 09:58:39.453275+02:00)
2019-09-08 09:58:39 | WARNING | Which interval(s) caused this job to execute? I only got args: (), kwargs: {'a': 123, 'b': 345}
2019-09-08 09:58:39 | INFO | Job "test_func (trigger: or[interval[0:00:10], interval[0:00:15], interval[0:00:50]], next run at: 2019-09-08 09:58:39 CEST)" executed successfully
2019-09-08 09:58:39 | DEBUG | Next wakeup is due at 2019-09-08 09:58:49.453275+02:00 (in 9.997841 seconds)

2019-09-08 09:58:49 | DEBUG | Looking for jobs to run
2019-09-08 09:58:49 | INFO | Running job "test_func (trigger: or[interval[0:00:10], interval[0:00:15], interval[0:00:50]], next run at: 2019-09-08 09:58:49 CEST)" (scheduled at 2019-09-08 09:58:49.453275+02:00)
2019-09-08 09:58:49 | WARNING | Which interval(s) caused this job to execute? I only got args: (), kwargs: {'a': 123, 'b': 345}
2019-09-08 09:58:49 | INFO | Job "test_func (trigger: or[interval[0:00:10], interval[0:00:15], interval[0:00:50]], next run at: 2019-09-08 09:58:49 CEST)" executed successfully
2019-09-08 09:58:49 | DEBUG | Next wakeup is due at 2019-09-08 09:58:59.453275+02:00 (in 9.997944 seconds)

2019-09-08 09:58:59 | DEBUG | Looking for jobs to run
2019-09-08 09:58:59 | INFO | Running job "test_func (trigger: or[interval[0:00:10], interval[0:00:15], interval[0:00:50]], next run at: 2019-09-08 09:58:59 CEST)" (scheduled at 2019-09-08 09:58:59.453275+02:00)
2019-09-08 09:58:59 | WARNING | Which interval(s) caused this job to execute? I only got args: (), kwargs: {'a': 123, 'b': 345}
2019-09-08 09:58:59 | INFO | Job "test_func (trigger: or[interval[0:00:10], interval[0:00:15], interval[0:00:50]], next run at: 2019-09-08 09:58:59 CEST)" executed successfully
2019-09-08 09:58:59 | DEBUG | Next wakeup is due at 2019-09-08 09:59:09.453275+02:00 (in 9.997905 seconds)

2019-09-08 09:59:00 | INFO | Got exit signal, exiting.

编辑:注意,如输出所示,间隔不正确(调度程序每次等待10秒,而它应该先等待10秒,然后等待5秒,然后等待5秒,然后等待10秒…)-但我想这是一个单独的问题。

根据文档,在组合触发器时

始终返回所有给定触发器可以约定的最早下次触发时间。当任何给定触发器已完成其计划时,触发器被视为已完成

使用OR时,只需一个为真

此外,interval触发器通过将间隔添加到执行时的当前时间来创建下一个激发时间

尽可能严格地解释这一点,这意味着在您的示例中,它将始终只使用10秒触发器

至于获取触发器,触发器是作业对象的一个属性,因此不能从函数访问它。即使如此,使用
job=scheduler.get\u job(id)
通过其id获取作业对象,然后使用
trigger=job.trigger
将提供您定义的整个组合触发器