Last active
August 29, 2015 13:58
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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