blob: 06e2b8cafaa66ab853d02d0a3ccc89ddb7efecd3 [file] [log] [blame]
Vince Harron05df6982015-05-13 05:00:23 +00001"""
2Interprocess mutex based on file locks
3"""
4
5import fcntl
6import os
7
8class Lock:
9
10 def __init__(self, filename):
11 self.filename = filename
12 # This will create it if it does not exist already
13 self.handle = open(filename, 'w')
14
15 # Bitwise OR fcntl.LOCK_NB if you need a non-blocking lock
16 def acquire(self):
17 fcntl.flock(self.handle, fcntl.LOCK_EX)
18
19 def release(self):
20 fcntl.flock(self.handle, fcntl.LOCK_UN)
21
22 def __del__(self):
23 self.handle.close()