File size: 655 Bytes
9de9fbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import fcntl


class FileLock:
    """
    A file lock class.
    """
    def __init__(self, filename):
        self.filename = filename
        self.handle = None

    def acquire_read_lock(self):
        self.handle = open(self.filename + '.lock', 'r')
        fcntl.flock(self.handle, fcntl.LOCK_SH | fcntl.LOCK_NB)

    def acquire_write_lock(self):
        self.handle = open(self.filename + '.lock', 'w')
        fcntl.flock(self.handle, fcntl.LOCK_EX | fcntl.LOCK_NB)

    def release_lock(self):
        if self.handle is not None:
            fcntl.flock(self.handle, fcntl.LOCK_UN)
            self.handle.close()
            self.handle = None