13

I'm trying to schedule programmatically some jobs with Advace Python Scheduler, my problem is that in the documentation it is only mentioned how to schedule with 'interval' trigger type, what about 'cron' and 'date'. Is there any complete documentation about scheduling options of APScheduler?

For instance:

#!/usr/bin/env python

from time import sleep 
from apscheduler.scheduler import Scheduler

sched = Scheduler()
sched.start()        

# define the function that is to be executed
def my_job(text):
    print text

job = sched.add_job(my_job, 'interval', id='my_job', seconds=10, replace_existing=True, args=['job executed!!!!'])

while True:
        sleep(1)

How I can schedule based on 'date' or 'cron'

I'm using latest APScheduler version 3.0.2

Thanks

tbo
  • 9,398
  • 8
  • 40
  • 51

2 Answers2

30
sched.add_job(my_job, trigger='cron', hour='22', minute='30')

Means call function 'my_job' once every day on 22:30.

APScheduler is a good stuff, but lack of docs, which is a pity, you can read the source codes to learn more.

There is some more tips for you:

  1. use *

    sched.add_job(my_job, trigger='cron', second='*') # trigger every second.
    
  2. some more attributes

    {'year': '*', 'month': 1, 'day': 1, 'week': '*', 'day_of_week': '*', 'hour': 0, 'minute': 0, 'second': 0}
    

And in my opinion, cron job can replace date jobs in most situations.

flycee
  • 11,948
  • 3
  • 19
  • 14
  • flycee thanks! yes eventually I started reading the source and found my way – tbo Apr 03 '15 at 11:34
  • 14
    Lack of docs? Excuse me? http://apscheduler.readthedocs.org/en/latest/modules/triggers/cron.html#module-apscheduler.triggers.cron http://apscheduler.readthedocs.org/en/latest/modules/triggers/date.html#module-apscheduler.triggers.date and http://apscheduler.readthedocs.org/en/latest/modules/triggers/interval.html#module-apscheduler.triggers.interval – Alex Grönholm Apr 04 '15 at 20:36
1

Based on date

job = sched.add_date_job(my_job, '2013-08-05 23:47:05', ['text']) # or can pass datetime object.

For example

import datetime
from datetime import timedelta
>>>job = sched.add_date_job(my_job, datetime.datetime.now()+timedelta(seconds=10), ['text'])
'text' # after 10 seconds

Based on cron

>>>job = sched.add_cron_job(my_job, second='*/5',args=['text'])
'text' every 5 seconds

Another example

>>>job = sched.add_cron_job(my_job,day_of_week='mon-fri', hour=17,args=['text'])
"text"  #This job is run every weekday at 5pm
itzMEonTV
  • 19,851
  • 4
  • 39
  • 49