I want to pause my code every hour mark, i.e 11:00pm, 12:00am, 1:00am, etc.
How do I go about doing this?
- You need to explain more about what you are doing. How long will it pause? How long will it run? Might it be a better idea to use a scheduler such as
cron, to run your task at a set period, rather than having it run constantly and sleep?Paul Rooney– Paul Rooney2017-08-11 03:23:01 +00:00CommentedAug 11, 2017 at 3:23
1 Answer1
You have two ways to do this:
cron method
Add a cron job for your script that gets executed every hour:
@hourly python /path/to/scriptcode method
Create an infinite loop within your code that runs every hour (have it sleep for 60 minutes). Or, create an infinite loop within your code that sleeps every minute, have it periodically check what time it is, and if it's on the hour with different delta hour than the current hour, have it run the job.
import datetimeimport timedelta_hour = 0while: now_hour = datetime.datetime.now().hour if delta_hour != now_hour: # run your code delta_hour = now_hour time.sleep(60) # 60 seconds # add some way to exit the infinite loop Sign up to request clarification or add additional context in comments.
Comments
Explore related questions
See similar questions with these tags.