Skip to content

Instantly share code, notes, and snippets.

@mschubert
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mschubert/10020547 to your computer and use it in GitHub Desktop.
Save mschubert/10020547 to your computer and use it in GitHub Desktop.
When starting a command using delay.py, delays the start of subsequent calls. This can be useful to e.g. limit peak file system load on a computing cluster.
#!/usr/bin/env python2.7
import sys, os.path
import subprocess
import time
import lockfile
import threading
lock = lockfile.FileLock(os.path.join(os.path.expanduser("~"), ".delay"))
lock.acquire()
# start thread that locks the file for delay seconds
def sleepDelay():
with lock:
time.sleep(float(sys.argv[1]))
th = threading.Thread(target=sleepDelay)
th.start()
# if command given, run it
if (len(sys.argv) > 2):
p = subprocess.Popen(" ".join(sys.argv[2:]),
stdin=sys.stdin,
stdout=sys.stdout,
stderr=sys.stderr,
shell=True)
p.wait()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment