m1k3y3
m1k3y3

Reputation: 2798

python: advanced python scheduler - cron style scheduling - pass function parameters

I am trying to schedule con style job using advanced python scheduler. Everything is fine whenever I use function that does not require any parameters, however I am unable to schedule the same job passing one or more arguments. Could you please advise?

from apscheduler.scheduler import Scheduler

def job_def(var1, var2):
    print "%s - %s" % (str(var1), str(var2))


s = Scheduler()

s.add_cron_job(job_def,['hello', 'there'], seconds='*/30')

s.start()

Error:

Traceback (most recent call last):   File "<stdin>", line 1, in <module> 

File "/usr/local/lib/python2.7/dist-packages/APScheduler-2.0.2-py2.7.egg/apscheduler scheduler.py", line 346, in add_cron_job start_date=start_date)   

File "/usr/local/lib/python2.7/dist-packages/APScheduler-2.0.2-py2.7.egg/apscheduler/triggers/cron/__init__.py", line 44, in __init__  field = field_class(field_name, exprs, is_default)   

File "/usr/local/lib/python2.7/dist-packages/APScheduler-2.0.2-py2.7.egg/apscheduler/triggers/cron/fields.py", line 29, in __init__ self.compile_expressions(exprs)   

File "/usr/local/lib/python2.7/dist-packages/APScheduler-2.0.2-py2.7.egg/apscheduler/triggers/cron/fields.py", line 56, in compile_expressions self.compile_expression(expr)   

File "/usr/local/lib/python2.7/dist-packages/APScheduler-2.0.2-py2.7.egg/apscheduler/triggers/cron/fields.py", line 69, in compile_expression (expr, self.name))

Upvotes: 3

Views: 6961

Answers (2)

user2589273
user2589273

Reputation: 2467

Would it not be easier to use

s.add_interval_job(job_def,args=['hello','there'],seconds=30)

instead?

Upvotes: 2

C&#233;dric Julien
C&#233;dric Julien

Reputation: 80801

First, according to the documentation, it should be second="*/30" instead of seconds="*30/", then, the add_cron_job method prototype is :

    def add_cron_job(self, func, year=None, month=None, day=None,
 week=None, day_of_week=None, hour=None, minute=None, second=None,
 start_date=None, args=None, kwargs=None, **options):

         """
         Schedules a job to be completed on times that match the given
         expressions.

         :param func: callable to run
         :param year: year to run on
         :param month: month to run on
         :param day: day of month to run on
         :param week: week of the year to run on
         :param day_of_week: weekday to run on (0 = Monday)
         :param hour: hour to run on
         :param second: second to run on
         :param args: list of positional arguments to call func with
         :param kwargs: dict of keyword arguments to call func with
         :param name: name of the job
         :param jobstore: alias of the job store to add the job to
         :param misfire_grace_time: seconds after the designated run time that
             the job is still allowed to be run
         :return: the scheduled job
         :rtype: :class:`~apscheduler.job.Job`
         """

so, in your case, you should write something like this :

from apscheduler.scheduler import Scheduler

def job_def(var1, var2):
    print "%s - %s" % (str(var1), str(var2))


s = Scheduler()

s.add_cron_job(job_def, args=['hello', 'there'], second='*/30')

s.start()

Upvotes: 7

Related Questions