src/pyams_utils/lock.py
changeset 62 52a07a902854
child 72 9049384a2bd4
equal deleted inserted replaced
61:6a579f05c692 62:52a07a902854
       
     1 #
       
     2 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
       
     3 # All Rights Reserved.
       
     4 #
       
     5 # This software is subject to the provisions of the Zope Public License,
       
     6 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    10 # FOR A PARTICULAR PURPOSE.
       
    11 #
       
    12 
       
    13 __docformat__ = 'restructuredtext'
       
    14 
       
    15 
       
    16 # import standard library
       
    17 import time
       
    18 from threading import local
       
    19 
       
    20 # import interfaces
       
    21 
       
    22 # import packages
       
    23 from beaker import cache
       
    24 
       
    25 
       
    26 _local = local()
       
    27 
       
    28 
       
    29 def get_locks_cache():
       
    30     """Get locks shared cache"""
       
    31     try:
       
    32         locks_cache = _local.locks_cache
       
    33     except AttributeError:
       
    34         manager = cache.CacheManager(**cache.cache_regions['persistent'])
       
    35         locks_cache = _local.locks_cache = manager.get_cache('PyAMS::locks')
       
    36     return locks_cache
       
    37 
       
    38 
       
    39 class LockException(Exception):
       
    40     """Cache lock exception"""
       
    41 
       
    42 
       
    43 class CacheLock(object):
       
    44     """Beaker based lock"""
       
    45 
       
    46     def __init__(self, name, wait=True):
       
    47         self.key = 'PyAMS_lock::{0}'.format(name)
       
    48         self.wait = wait
       
    49         self.has_lock = False
       
    50 
       
    51     def __enter__(self):
       
    52         locks_cache = get_locks_cache()
       
    53         while True:
       
    54             test = locks_cache.has_key(self.key)
       
    55             if test:
       
    56                 if not self.wait:
       
    57                     raise LockException()
       
    58                 else:
       
    59                     time.sleep(0.1)
       
    60             else:
       
    61                 locks_cache.set_value(self.key, 1)
       
    62                 self.has_lock = True
       
    63                 return
       
    64 
       
    65     def __exit__(self, exc_type, exc_val, exc_tb):
       
    66         if self.has_lock:
       
    67             get_locks_cache().remove_value(self.key)
       
    68         return False
       
    69 
       
    70 
       
    71 def locked(name, wait=True):
       
    72     """Locked function decorator"""
       
    73 
       
    74     def lock_decorator(func):
       
    75         def wrapper(*args, **kwargs):
       
    76             with CacheLock(name, wait):
       
    77                 return func(*args, **kwargs)
       
    78         return wrapper
       
    79 
       
    80     return lock_decorator