【问题标题】:python: advanced python scheduler - cron style scheduling - pass function parameterspython:高级python调度器——cron风格调度——传递函数参数
【发布时间】:2011-12-20 07:30:12
【问题描述】:

我正在尝试使用高级 python 调度程序来安排 con 风格的作业。每当我使用不需要任何参数的函数时,一切都很好,但是我无法安排传递一个或多个参数的相同作业。可以请教吗?

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()

错误:

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))

【问题讨论】:

    标签: python cron scheduler


    【解决方案1】:

    首先,根据to the documentation,应该是second="*/30"而不是seconds="*30/",然后是add_cron_jobmethod 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`
             """
    

    所以,在你的情况下,你应该这样写:

    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()
    

    【讨论】:

    • @m1k3y02 :我编辑了我的答案并正确调用add_cron_job
    • 就像一个魅力!我想我缺少一些关于如何阅读文档的基础知识
    【解决方案2】:

    用起来会不会更方便

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

    改为?

    【讨论】:

    • - seconds=30 将是 00:00:30, 00:01:30 每分钟执行一次。 - seconds=*/30 包括 0,所以 00:00:00、00:00:30、00:01:00、00:01:30,每 30 秒
    猜你喜欢
    • 1970-01-01
    • 2012-05-03
    • 2013-02-15
    • 1970-01-01
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-27
    相关资源
    最近更新 更多