From 6df2ca02df8b07650b708491db8001c07cb72061 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Wed, 11 May 2011 16:19:20 +0200 Subject: Fix concurrency between declarations for LazyAttributes and Sequence. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Raphaƫl Barrois --- factory/declarations.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'factory') diff --git a/factory/declarations.py b/factory/declarations.py index cf54f07..4c44e0c 100644 --- a/factory/declarations.py +++ b/factory/declarations.py @@ -18,6 +18,23 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. +import threading + +global_counter_lock = threading.Lock() + +class GlobalCounter(object): + """A simple global counter.""" + + _value = 0 + + @classmethod + def step(cls): + with global_counter_lock: + current = cls._value + cls._value += 1 + return current + + class OrderedDeclaration(object): '''A factory declaration. @@ -26,13 +43,7 @@ class OrderedDeclaration(object): _next_order = 0 def __init__(self): - self.order = self.next_order() - - @classmethod - def next_order(cls): - next_order = cls._next_order - cls._next_order += 1 - return next_order + self.order = GlobalCounter.step() def evaluate(self, factory, attributes): '''Evaluate this declaration. -- cgit v1.2.3