Skip to content
Advertisement

Python does something every 5 minutes

i need to check data on an API. The API is refreshed with new data every 5 minutes (10:00, 10:05, 10:10 etc…)

I don’t want to use time.sleep(300) because i want my script to do something at 10:05:03, then 10:05:03 etc. and not 5 min avec the script started (maybe it started at 10h12 How can i build this?

Thanks y’all.

Advertisement

Answer

UPDATE:

Just wanted to remove the possibility of recursion error, so I have rewritten the code:

JavaScript

Also if you want your thread to quit when the main program exits you could set daemon as True on the thread like:

JavaScript

But this does not enforce a clean termination of this thread so you could also try this approach below:

JavaScript


You can use the following code:

JavaScript

This will call your check_api function every 5 minutes and will not block your main code’s execution.

as mentioned by @scotyy3785 the above code will only run once but I realize what you want and have written the code for it:

JavaScript

The above code will call check_api at minutes like 00, 05, 10, 15…

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement