Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass parameters to schedule

How can I pass parameters to schedule?

The function I want to get called:

def job(param1, param2):
    print(str(param1) + str(param2))

How I schedule it:

schedule.every(10).minutes.do(job)

How can I pass a parameter to do(job)?

like image 614
Adityo Setyonugroho Avatar asked Nov 24 '16 08:11

Adityo Setyonugroho


People also ask

What is schedule Run_pending ()?

run_pending() : Calls run_pending on the default scheduler instance. Run all jobs that are scheduled to run.

How do you stop a scheduled job in Python?

The cancel() method of the scheduler class from the Python Standard Library module sched, removes an already scheduled event from a scheduler instance.


2 Answers

In general with this kind of thing you can always do this:

schedule.every(10).minutes.do(lambda: job('Hello ', 'world!'))

Looking at the source:

def do(self, job_func, *args, **kwargs):
    """Specifies the job_func that should be called every time the
    job runs.
    Any additional arguments are passed on to job_func when
    the job runs.
    :param job_func: The function to be scheduled
    :return: The invoked job instance
    """
    self.job_func = functools.partial(job_func, *args, **kwargs)

We see that you can also do this:

schedule.every(10).minutes.do(job, 'Hello ', 'world!')

Or, as the code suggests, an alternative to the generic lambda method is:

schedule.every(10).minutes.do(functools.partial(job, 'Hello ', 'world!'))
like image 185
Alex Hall Avatar answered Sep 28 '22 16:09

Alex Hall


schedule.every(10).minutes.do(job, 'do something', 'like this')

My friend tried the above syntax but it didn't work, so if it does not work you can use

schedule.every(10).minutes.do(job('do something', 'like this'))

His schedule statement was not like this exactly it was like this -

schedule.every().monday.at('8:30')do(job('It was', 'like this'))
like image 1
Phenomenal Physics Avatar answered Sep 28 '22 17:09

Phenomenal Physics