20 lines
437 B
Python
20 lines
437 B
Python
# -*- coding: utf-8 -*-
|
|
|
|
import fcntl
|
|
|
|
from contextlib import contextmanager
|
|
|
|
def locked_ex(lock_file):
|
|
def decorator(target):
|
|
def wrapper(*args, **kwargs):
|
|
with lock_ex(lock_file):
|
|
return target(*args, **kwargs)
|
|
return wrapper
|
|
return decorator
|
|
|
|
@contextmanager
|
|
def lock_ex(lock_file):
|
|
with open(lock_file, 'w') as lock:
|
|
fcntl.lockf(lock, fcntl.LOCK_EX)
|
|
yield lock
|