1

I want to schedule and run 2 functions

func1 to run for a specific duration func2 to run for a specific duration

time1 = 4:20
time2 = 6:20
duration1 = 1 hour
duration2 = 1 hour

def func1():
    print("func1") # random function

def func2():
    print("func2") # random function

I just gave an idea of the code, it would be a great help solving it...

2 Answers 2

1

I'll try to fulfil your requirements...

So you'll need a code to run func1 at a specific time and till the specific duration then run func2 at a specific time and till the specific duration....

according to that I'll import schedule and time.

import schedule  
import time

# make sure the time is in correct format....If the time is 4:20, then you must enter 04:20...
time1 = input("When do you want to start func1 ? (format=00:00): ")

# 1 hour = 3600
# 1 hour, 15 mins = 4500
# 1 hour, 30 mins = 5400

# make sure the duration is only in seconds... for 1 hour you must enter 3600
duration1 = input("What will be the duration of func1 ? (format = only seconds): ")


# make sure the time is in correct format if the time is 6:20 then enter 06:20
time2 = input("when do you want to start func2 ? (format=00:00): ")

# make sure the duration is only in seconds... for 1 hour you must enter 3600
duration2 = input("what will be the duration of the func2? (format = only seconds): ")



def func1():
    # your func1
    print("func1") #example

    time.sleep(int(duration1))

def func2():
    #your func2
    print("func2") #example

    time.sleep(int(duration2))
    exit()


def looping():
    schedule.every().day.at(time2).do(func2)
    while True:
        schedule.run_pending()
        time.sleep(1)


schedule.every().day.at(time1).do(func1)
while True:
    looping()

I tried to give my best according to your requirements, hope this helps you...

Sign up to request clarification or add additional context in comments.

Comments

0

You can use time.sleep() function from time module if that's what you are looking for.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.