Seven-claw source code: Python program tutorial that runs at regular intervals using the time and datetime modules
This tutorial will teach you how to use the time and datetime modules to set up a Python program to run periodically. This is useful if you want to check something frequently, so you have a python program running in the background that does it for you periodically.
solution
I'll put the solution first. If you're interested in how I found it, you can read about it below.
If what you're doing is a lightweight simple solution
Lightweight means it doesn't use as much CPU power and time as a simple print function.
Suppose you want to run your code every 10 seconds.
you should get something like this
With that, you can replace the sleep time if you want a different interval, and you can replace the contents of the do_things() function with what you want the program to do.
Simple solution to the problem
But if you're like me, what you want to do every interval takes time, whether it's waiting for I/O from another program, or doing something CPU-intensive. Then, if we still do what we do, we'll get inaccuracy in the wait time as shown below.
Here I use sum(i*i for i in range(number)) to force the CPU to do a lot of work on a huge number, don't mind the underscore in 50_000_000, it's the same as 50000000. The result of this program is:
As you can see, we want the program to execute every 10 seconds, but the program takes about 14 seconds per loop because the calculation takes some extra time. That's why you need advanced solutions.
Advanced Solutions
> > import time from datetime import datetime, timedelta number = 50_000_000 sum(i*i for i in range(number)) print(datetime.now()) while True: previous_datetime = datetime.now() do_things() time_took_running_do_things = datetime.now() - previous_datetime remaining_time_in_secs = (timedelta(seconds=10) - time_took_running_do_things).total_seconds() time.sleep(remaining_time_in_secs) >
turn out:
explain
Here you can see that the code runs every 10 seconds even though we are doing the heavy lifting. This is because we compensate for the time it takes to run do_things() by waiting less time. If the program takes 3 seconds to run do_things(), then we only wait 7 seconds after that, so the program still executes every 10 seconds.
limitation
This solution is limited, what you want to do must take longer than the interval you want to loop over. If you want to do an operation that takes 10 seconds every 1 second, this solution won't help you.
how did i get there
Now I'll talk about how I solved this problem.
I'm working on a program that sees if I play computer games regularly to remind myself how long I've been playing, but the problem is it's very inaccurate. I would play a game for 45 minutes and the program would tell me I only played 30 minutes.
I thought it was a problem with time.sleep() at first, maybe because the CPU was doing a lot of work while playing the game and the wait was wrong.
But it turned out to be wrong, the program still runs at incorrect intervals when I'm not playing the game.
Then I commented out what the program did in each interval, leaving only the print time statement, and the problem was solved. It turns out that the problem was that what I was doing in the interval took more than 2 seconds!
Then I immediately proceeded to try overengineering the problem. I thought to myself, maybe I want to put the loop and the do_things() function in separate threads via threading or async, or even use multiprocessing to separate the CPU. I watched Jim Anderson's entire tutorial on using concurrency to speed up Python programs on RealPython until I drew this diagram.
Then I realized that if I just do them linearly and then calculate how much time I need to wait? It dawned on me then that things have never been more complicated. So I solved this problem and decided to write this article to help people like me. So there you have it.
Latest Programming News and Information | GeekBar