From b99def91287d04c1ca7b245f878f9dbdb522ebdb Mon Sep 17 00:00:00 2001 From: Fabio Erculiani Date: Tue, 10 Dec 2013 11:22:42 +0100 Subject: [PATCH] [entropy.locks] add exclusive() and shared() context managers --- lib/entropy/locks.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/entropy/locks.py b/lib/entropy/locks.py index f5998dc64..e78b8a60a 100644 --- a/lib/entropy/locks.py +++ b/lib/entropy/locks.py @@ -9,6 +9,7 @@ B{Entropy resources lock functions module}. """ +import contextlib import errno import fcntl import threading @@ -292,6 +293,17 @@ class ResourceLock(object): self.acquire_exclusive() return True + @contextlib.contextmanager + def exclusive(self): + """ + Acquire an exclusive file lock for this repository (context manager). + """ + self.wait_exclusive() + try: + yield + finally: + self.release() + def acquire_exclusive(self): """ Acquire the resource in exclusive blocking mode. @@ -313,6 +325,17 @@ class ResourceLock(object): """ return self._wait_resource(False) + @contextlib.contextmanager + def shared(self): + """ + Acquire a shared file lock for this repository (context manager). + """ + self.wait_shared() + try: + yield + finally: + self.release() + def acquire_shared(self): """ Acquire the resource in shared blocking mode.