Created
February 1, 2023 19:39
-
-
Save tsibley/9d495ed6d35432a5c3ff59af86e40e2b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import os | |
from contextlib import contextmanager | |
from time import sleep, time | |
@contextmanager | |
def Lockfile(path, timeout = 5 * 60): | |
started = time() | |
while (time() - started) < timeout: | |
try: | |
lock = os.open(path, os.O_RDONLY | os.O_CREAT | os.O_EXCL) | |
except FileExistsError: | |
# didn't get the lock; try again in 0.1s. | |
sleep(0.1) | |
else: | |
break | |
else: | |
raise TimeoutError(f"couldn't acquire lock within {timeout}s") | |
try: | |
yield | |
finally: | |
os.close(lock) | |
os.unlink(path) | |
rule deploy: | |
input: expand("{x}.deploy", x=list("abcd")) | |
rule deploy_one: | |
output: "{x}.deploy" | |
run: | |
with Lockfile("deploy.lock"): | |
shell(""" | |
sleep 2 | |
touch {output:q} | |
""") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment