Search code examples
pythonapifunction-pointerscron-task

function pointer with parameters in python?


I'm reading the docs from pythons Cron-job (Example 1):

from apscheduler.scheduler import Scheduler

# Start the scheduler
sched = Scheduler()
sched.start()

def job_function():
    print "Hello World"

# Schedules job_function to be run on the third Friday
# of June, July, August, November and December at 00:00, 01:00, 02:00 and 03:00
sched.add_cron_job(job_function, month='6-8,11-12', day='3rd fri', hour='0-3') 

is it possible to add paremters to that job_function and how to pass them to add_cron_job?


Solution

  • Use the args keyword argument:

    # call job_function with arguments 'hello' and 'world'
    sched.add_cron_job(job_function, args=('hello', 'world'), month='1', day='1st fri', hour='0') 
    

    See the definition of add_cron_job.