dbader / schedule

Python job scheduling for humans.
https://schedule.readthedocs.io/
MIT License
11.79k stars 961 forks source link

Jobs dependency - How to create schedule with jobs dependency? #215

Open edilsonpecanha opened 6 years ago

edilsonpecanha commented 6 years ago

How to create schedule with jobs dependency? Example: job2 execute only if job1 executed with success.

I try do this, but not working:

import schedule
import time

def now():
    return time.strftime("%H:%M:%S")

def job1():
    print("Working 10..." + now())
    time.sleep(10)
    print("Working 11..." + now())

def job2():
    print("Working 20..." + now())
    time.sleep(10)
    print("Working 21..." + now())

schedule.every(30).seconds.do(job1).do(job2)

The program execute only last "do". Result of execution: I'm working 20...17:39:34 I'm working 21...17:39:44 I'm working 20...17:39:44 I'm working 21...17:39:54 I'm working 20...17:39:54 I'm working 21...17:40:04

nickbean01 commented 6 years ago

You can just create a new function that has both job1() and job2()

import time

def now():
    return time.strftime("%H:%M:%S")

def job1():
    print("Working 10..." + now())
    time.sleep(10)
    print("Working 11..." + now())

def job2():
    print("Working 20..." + now())
    time.sleep(10)
    print("Working 21..." + now())

def jobs():
    job1()
    job2()

schedule.every(30).seconds.do(jobs)