summaryrefslogtreecommitdiff
path: root/client/utils/locking.py
diff options
context:
space:
mode:
authorTomas Vondra2016-08-10 21:23:55 +0000
committerTomas Vondra2017-02-27 00:21:09 +0000
commit72e6220f64a89cd215660311a5680f07f543b150 (patch)
treec76f2df22da3819a28cad200f4b2a45642dfacdf /client/utils/locking.py
parentcbac00d3965ad4f27f1e812668b5732c1c50b1dd (diff)
Import initial version of the client
Diffstat (limited to 'client/utils/locking.py')
-rw-r--r--client/utils/locking.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/client/utils/locking.py b/client/utils/locking.py
new file mode 100644
index 0000000..dfc8f63
--- /dev/null
+++ b/client/utils/locking.py
@@ -0,0 +1,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)