[PYTHON] A little advanced job scheduling with AP Scheduler

A little advanced job scheduling with AP Scheduler

Reason, purpose and motive

Searching for a job scheduler that can prevent multiple startups

Installation

With pip or easy_install.

$ easy_install apscheduler
Or
$ pip install apscheduler

Let's write a job scheduler

import apscheduler.scheduler as ApSched

def job_function():
    print "Hello World"

sched = ApSched.Scheduler(standalone=True,coalesce=True)
sched.add_interval_job(job_function, seconds=10)
sched.start()

The standalone option is False by default. If set to True, the scheduler itself will run demonic.

The result of trying it with ipython.

In [18]: 2013-11-26 16:12:32.680493
Hello World
In [18]: 2013-11-26 16:12:42.680962
Hello World
In [18]: 2013-11-26 16:12:52.680975
Hello World

In addition, the interval time is calculated by properly omitting the program execution time.

Experiment to prevent multiple activation

from apscheduler.scheduler import Scheduler
import datetime
import time

def job_function_13sec():
    print datetime.datetime.today()
    time.sleep(13)
    print "Hello World"

sched = Scheduler(standalone=True,coalesce=True)
sched.add_interval_job(job_function_13sec, seconds=10)
sched.start()

result.

In [46]: sched.start()
2013-11-26 16:36:10.818178
WARNING:apscheduler.scheduler:Execution of job "job_function_13sec (trigger: interval[0:00:10], next run at: 2013-11-26 16:36:30.817553)" skipped: maximum number of running instances reached (1)
Hello World
2013-11-26 16:36:30.818776
WARNING:apscheduler.scheduler:Execution of job "job_function_13sec (trigger: interval[0:00:10], next run at: 2013-11-26 16:36:40.817553)" skipped: maximum number of running instances reached (1)
Hello World
2013-11-26 16:36:50.818992
WARNING:apscheduler.scheduler:Execution of job "job_function_13sec (trigger: interval[0:00:10], next run at: 2013-11-26 16:37:10.817553)" skipped: maximum number of running instances reached (1)
Hello World
2013-11-26 16:37:10.818815
WARNING:apscheduler.scheduler:Execution of job "job_function_13sec (trigger: interval[0:00:10], next run at: 2013-11-26 16:37:30.817553)" skipped: maximum number of running instances reached (1)
Hello World
2013-11-26 16:37:30.818828

As a job option __ By default max_run is 1__, which prevents multiple launches.

afterwards

Recommended Posts

A little advanced job scheduling with AP Scheduler
A little stuck with chainer
Start a process with a scheduling policy on Linux
Python Ver. To introduce WebPay with a little code.
[PyTorch] A little understanding of CrossEntropyLoss with mathematical formulas
A story linked with Google Cloud Storage with a little ingenuity