0

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??

gariepy
  • 3,576
  • 6
  • 21
  • 34
Laxmi Kadariya
  • 1,103
  • 1
  • 14
  • 34

1 Answers1

3

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?

Alex Grönholm
  • 5,563
  • 29
  • 32