在python中的APScheduler库中多次调用预定作业函数

时间:2015-12-01 05:32:31

标签: python apscheduler

我在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

在执行时间满足时多次。

我对调度程序的行为感到困惑。如何在调度时间满足时多次调用该函数?

1 个答案:

答案 0 :(得分:2)

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