【问题标题】:scheduled jobs function is called multiple times in APScheduler library in python在 python 的 APScheduler 库中多次调用计划作业函数
【发布时间】:2015-12-01 05:32:31
【问题描述】:

我在 python 的 apscheduler 库中出现了意外行为。

这里我有一个简单的代码来调用基本函数:

import sys
from time import sleep
from datetime import datetime
from apscheduler.scheduler import Scheduler
import logging
logging.basicConfig()
count = 0;

     # start the scheduler

# define the function that is to be executed
# it will be executed in a thread by the scheduler
def my_job():



    print "hello world"


def main():

    sched = None


    while True:


        sched=Scheduler()

        sched.start()

        sched.add_cron_job(my_job,second=3)

        sched.print_jobs()  


        sleep(5)
        sys.stdout.write('.'); sys.stdout.flush()


##############################################################

if __name__ == "__main__":
    main()

这里我有一个打印简单字符串的函数,我想让这个调用这个 my_job 所以我使用调度程序调用了一个 my-func

sched.add_cron_job(my_job,second=3)

程序在每分钟的第三秒调用所需的函数。它将多个 hello world 打印为

hello world
hello world
hello world

多次执行时满足。

我对调度程序的行为感到困惑。调度时间到了怎么能多次调用函数??

【问题讨论】:

    标签: python apscheduler


    【解决方案1】:

    它会打印多个 hello world,因为您在无限循环的每次迭代中都创建了一个新的调度程序。您是否希望先前迭代中的调度程序在您创建新的迭代时停止?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-20
      • 1970-01-01
      • 2016-03-17
      • 2020-06-25
      • 1970-01-01
      相关资源
      最近更新 更多