Python Apscheduler 条件任务

标签 python apscheduler

我有一组简单的函数 foobar。我想仅在成功完成任务/函数 foo 后运行第二个作业 bar

目前我正在使用全局变量来执行此操作:

from apscheduler.scheduler import Scheduler

success = 0

def foo():
    global success
    try:
        print 'yes'
    except:
        success = 0
        return
    success = 1
    return

def bar():
    if success:
        print 'yes'
    else:
        print 'no'
    return

scheduler = Scheduler()
scheduler.add_cron_job(foo, day_of_week='mon-fri', hour=18, minute=30);
scheduler.add_cron_job(bar, day_of_week='mon-fri', hour=18, minute=45)
scheduler.start()

是否有更好的方法来使用 apscheduler 执行条件任务?

最佳答案

如果成功,则在 15 分钟内从 foo() 安排新的 bar() 运行。

像这样:

from datetime import datetime, timedelta

def foo():
    print 'yes'
    scheduler.add_date_job(bar, datetime.now() + timedelta(minutes=15))

关于Python Apscheduler 条件任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30202979/

相关文章:

soundcloud - Apscheduler 运行一次然后抛出 TypeError

python - 可作为 dict.get 的默认参数调用,如果键存在则不调用它

python - 将元素 append 到 numpy 数组中每个箭头的末尾

python - django 模型中的 m2m 'through' 字段抛出此错误 : 'M2M field' object has no attribute '_m2m_reverse_name_cache'

python - 将运行时值分配给 tensorflow 变量

python - APScheduler - 导入错误 : No module named 'apscheduler'

Python Apscheduler 循环中的 cron 作业并不执行所有不同版本

python - 使用 Sqlite 查询将 pandas 数据框中的数据插入到 Sqlite

具有多个实例的 Django 应用程序 - 如何确保每日电子邮件仅发送一次?

python-2.7 - APScheduler 中的 'Interval' 和 'Cron' 触发器有什么区别?