简体   繁体   中英

scheduled jobs function is called multiple times in APScheduler library in python

I have got unexpected behaviour in apscheduler library in python.

here I have a simple code to call the basic function as:

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()

Here I have a function to print simple string and I want to have this call this my_job so I called a my-func using scheduler as

sched.add_cron_job(my_job,second=3)

The program calls the desired function but during third second of every minute . It prints multiple hello world as

hello world
hello world
hello world

multiple times when the time of execution is meet.

I am confused with the behaviuour of scheduler. how can it call the function multiple times when the scheduling time is meet??

It prints multiple hello worlds because you are creating a new scheduler on every iteration in your infinite loop. Do you expect the schedulers from the previous iterations to just stop when you create a new one?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM