Race condition when adding a job once scheduler is started

chemicalstorm avatarchemicalstorm created an issue

Using the following code:

from apscheduler.scheduler import Scheduler
logging.basicConfig(level=logging.DEBUG)

def job_function():
    print "Hello World"
    time.sleep(10)

# Start the scheduler
sched = Scheduler()

sched.start()
sched.add_cron_job(job_function, **{'minute':'*/2'})

It seems we can fall in a race condition. Several launches of this code display different outputs (I just added a log trace in scheduler.py, function _real_add_job)

Functionnal:

guestdev@guestdev-laptop:~/Projet/bac_a_sable/APScheduler-2.0.1$ python test_aps.py 
INFO:apscheduler.threadpool:Started thread pool with 0 core threads and 20 maximum threads
INFO:apscheduler.scheduler:Scheduler started
DEBUG:apscheduler.scheduler:Looking for jobs to run
DEBUG:apscheduler.scheduler:No jobs; waiting until a job is added
DEBUG:apscheduler.scheduler:Adding real job with wakeup: True
INFO:apscheduler.scheduler:Added job "job_function (trigger: cron[minute='*/2'], next run at: 2011-06-20 13:32:00)" to job store "default"
DEBUG:apscheduler.scheduler:Looking for jobs to run
DEBUG:apscheduler.scheduler:Next wakeup is due at 2011-06-20 13:32:00 (in 100.794154 seconds)

Race condition, which leads to multiple fires of the same job:

guestdev@guestdev-laptop:~/Projet/bac_a_sable/APScheduler-2.0.1$ python test_aps.py 
INFO:apscheduler.threadpool:Started thread pool with 0 core threads and 20 maximum threads
INFO:apscheduler.scheduler:Scheduler started
DEBUG:apscheduler.scheduler:Adding real job with wakeup: True
DEBUG:apscheduler.scheduler:Looking for jobs to run
INFO:apscheduler.scheduler:Added job "job_function (trigger: cron[minute='*/2'], next run at: 2011-06-20 13:32:00)" to job store "default"
DEBUG:apscheduler.scheduler:Next wakeup is due at 2011-06-20 13:32:00 (in 99.725313 seconds)
DEBUG:apscheduler.scheduler:Looking for jobs to run
DEBUG:apscheduler.scheduler:Next wakeup is due at 2011-06-20 13:32:00 (in 99.724925 seconds)

I can't reproduce the bug if I add a job before starting the scheduler.

Comments (3)

  1. chemicalstorm

    Nervermind, I am such a dumbass... I was mislead by the scheduler waking up twice in my example, that I did not see I messed up my cronjob (not the one attached as example in my first post):

    sched.add_cron_job(job_function, **{'minute':'*/2', 'second':'*'})
    

    properly triggers the job every second....

    Anyway, thanks for your excellent work on this module! It really awesome and useful.

  2. Log in to comment
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.