blob: d3cbd64a03bfbe10426843b91115625eceb8ff53 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
import fcntl
import os
class FileLock():
'a simple wrapper around file lock'
def __init__(self, filename):
self._file = open(filename, 'w')
def __enter__(self):
'locks the file and writes the PID of the current process into it'
fcntl.flock(self._file, fcntl.LOCK_EX)
self._file.write(str(os.getpid()))
self._file.flush()
return self._file
def __exit__(self, type, value, traceback):
'unlock the file'
fcntl.flock(self._file, fcntl.LOCK_UN)
|