From 7d3ece502c747f80fbb2a9bcf70bdeda336bec75 Mon Sep 17 00:00:00 2001 From: Bastian Kleineidam Date: Tue, 9 Oct 2012 19:46:06 +0200 Subject: [PATCH] Support semaphores. --- linkcheck/lock.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/linkcheck/lock.py b/linkcheck/lock.py index e5c7ceec..f5a70d93 100644 --- a/linkcheck/lock.py +++ b/linkcheck/lock.py @@ -20,11 +20,13 @@ Locking utility class. import threading from . import log, LOG_THREAD -def get_lock (name): +def get_lock (name, debug=False): """Return a new thread lock object.""" - return threading.Lock() + lock = threading.Lock() # for thread debugging, use the DebugLock wrapper - #return DebugLock(threading.Lock(), name) + if debug: + lock = DebugLock(lock, name) + return lock class DebugLock (object): @@ -47,3 +49,14 @@ class DebugLock (object): threadname = threading.currentThread().getName() log.debug(LOG_THREAD, "Release %s for %s", self.name, threadname) self.lock.release() + + +def get_semaphore(name, value=None, debug=False): + if value is None: + lock = threading.Semaphore() + else: + lock = threading.BoundedSemaphore(value) + if debug: + lock = DebugLock(lock, name) + return lock +