0

Is it possible to make the script drop everything it's been doing if a timer hits a certain time?

Let's say I want it to run for 24 hours and after that it should print a few things and then exit.

1
  • 1
    time.sleep(24*60*60) Commented Jul 16, 2020 at 16:10

1 Answer 1

1

This is a pretty rude method but it will work. REMEMBER: put your code after the else statement or it won't work. Also, modify the comment above exit() as the comment said. PS.: this are built-in modules. No need to install anything.

    import datetime
    from sys import exit

    starting_time = datetime.datetime.now()

    while True:
        actual_datetime = datetime.datetime.now()
        if actual_datetime > starting_time + datetime.timedelta(days=1):
            # do what you need to do before it shut down
            exit()
        else:
            # RUN YOUR PROGRAM HERE
Sign up to request clarification or add additional context in comments.

1 Comment

Also, you can't use time.sleep() because it will only delay your current thread for a day and it won't tun your code in a "sort of parallel way"

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.