Skip to content

Instantly share code, notes, and snippets.

@thomasbrueggemann
Last active December 11, 2015 09:48
Show Gist options
  • Save thomasbrueggemann/4582638 to your computer and use it in GitHub Desktop.
Save thomasbrueggemann/4582638 to your computer and use it in GitHub Desktop.
Distributes the sleeping time of a script over the 24 hours of a day, so that at the zenith the max_time will be waited and 12 hours later 1 second will be waited. That gives to opportunity to set the zenit to the hour of the day that to least traffic will be expected (e.g. 3am). This could be interesting to any kind of long running scripts that…
from datetime import datetime
import math, time
#
# SINUS SLEEPER
#
# max_time: maximum seconds to sleep at the zenith hour
# zenith : hour (0-23) of day at which the max_time value is reached
#
def sinus_sleeper(max_time=120, zenith=0):
x = (datetime.now().hour - zenith) % 24
sleep_time = math.floor(1 - max_time * math.sin((1/(math.pi/24))*x) + (max_time + 1))
time.sleep(sleep_time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment