lwzm
9/12/2016 - 6:30 AM

File Lock

File Lock

import fcntl

class Lock():
    def __init__(self, fn=".lock"):
        self.fd = open(fn, "w")

    def __enter__(self):
        fcntl.lockf(self.fd, fcntl.LOCK_EX)

    def __exit__(self, type, value, traceback):
        fcntl.lockf(self.fd, fcntl.LOCK_UN)


def t():
    l = Lock()
    with l:
        input(1)
    time.sleep(0.1)
    with l:
        input(2)