This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
logtracker/logtracker/utils.py

16 lines
331 B
Python

import os
import sys
import fcntl
from contextlib import contextmanager
from io import BlockingIOError
@contextmanager
def lock(lockfile):
try:
handle = open(lockfile, 'w')
fcntl.lockf(handle, fcntl.LOCK_EX | fcntl.LOCK_NB)
yield
except BlockingIOError:
print('locked')
sys.exit(1)