Skip to content

Instantly share code, notes, and snippets.

@mothdotmonster
Created August 4, 2022 21:21
Show Gist options
  • Save mothdotmonster/b34be76e3df4c40cd6e04ad1a67f4cf0 to your computer and use it in GitHub Desktop.
Save mothdotmonster/b34be76e3df4c40cd6e04ad1a67f4cf0 to your computer and use it in GitHub Desktop.
sleep with random delay, so you don't overload the server at midnight
#!/usr/bin/env python
# fuzzysleep - sleep with random delay, so you don't overload the server at midnight
import time, random, argparse
parser = argparse.ArgumentParser()
parser.add_argument("delay", help="Desired maximum delay", type=int)
parser.add_argument("--verbose", help="Prints extra info", action="store_true")
group = parser.add_mutually_exclusive_group()
group.add_argument("--minutes", action="store_true", help="Use minutes instead of seconds")
group.add_argument("--hours", action="store_true", help="Use hours instead of seconds")
args=parser.parse_args()
if args.minutes:
delay = random.randint(0, (args.delay*60))
elif args.hours:
delay = random.randint(0, (args.delay*3600))
else:
delay = random.randint(0, args.delay)
if args.verbose:
print("Sleeping for {} seconds...".format(delay))
time.sleep(delay)
if args.verbose:
print("Sleeping finished!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment