本文整理汇总了Python中DIRAC.Core.Utilities.LockRing.LockRing.getLock方法的典型用法代码示例。如果您正苦于以下问题:Python LockRing.getLock方法的具体用法?Python LockRing.getLock怎么用?Python LockRing.getLock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DIRAC.Core.Utilities.LockRing.LockRing
的用法示例。
在下文中一共展示了LockRing.getLock方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from DIRAC.Core.Utilities.LockRing import LockRing [as 别名]
# 或者: from DIRAC.Core.Utilities.LockRing.LockRing import getLock [as 别名]
class Synchronizer:
""" Class encapsulating a lock
allowing it to be used as a synchronizing
decorator making the call thread-safe"""
def __init__(self, lockName="", recursive=False):
from DIRAC.Core.Utilities.LockRing import LockRing
self.__lockName = lockName
self.__lr = LockRing()
self.__lock = self.__lr.getLock(lockName, recursive=recursive)
def __call__(self, funcToCall):
def lockedFunc(*args, **kwargs):
try:
if self.__lockName:
print "LOCKING", self.__lockName
self.__lock.acquire()
return funcToCall(*args, **kwargs)
finally:
if self.__lockName:
print "UNLOCKING", self.__lockName
self.__lock.release()
return lockedFunc
def lock(self):
return self.__lock.acquire()
def unlock(self):
return self.__lock.release()
示例2: __init__
# 需要导入模块: from DIRAC.Core.Utilities.LockRing import LockRing [as 别名]
# 或者: from DIRAC.Core.Utilities.LockRing.LockRing import getLock [as 别名]
def __init__( self, loadDefaultCFG = True ):
lr = LockRing()
self.threadingEvent = lr.getEvent()
self.threadingEvent.set()
self.threadingLock = lr.getLock()
self.runningThreadsNumber = 0
self.compressedConfigurationData = ""
self.configurationPath = "/DIRAC/Configuration"
self.backupsDir = os.path.join( DIRAC.rootPath, "etc", "csbackup" )
self._isService = False
self.localCFG = CFG()
self.remoteCFG = CFG()
self.mergedCFG = CFG()
self.remoteServerList = []
if loadDefaultCFG:
defaultCFGFile = os.path.join( DIRAC.rootPath, "etc", "dirac.cfg" )
gLogger.debug( "dirac.cfg should be at", "%s" % defaultCFGFile )
retVal = self.loadFile( defaultCFGFile )
if not retVal[ 'OK' ]:
gLogger.warn( "Can't load %s file" % defaultCFGFile )
self.sync()
示例3: Cache
# 需要导入模块: from DIRAC.Core.Utilities.LockRing import LockRing [as 别名]
# 或者: from DIRAC.Core.Utilities.LockRing.LockRing import getLock [as 别名]
class Cache( object ):
"""
Cache basic class.
WARNING: None of its methods is thread safe. Acquire / Release lock when
using them !
"""
def __init__( self, lifeTime, updateFunc ):
"""
Constructor
:Parameters:
**lifeTime** - `int`
Lifetime of the elements in the cache ( seconds ! )
**updateFunc** - `function`
This function MUST return a S_OK | S_ERROR object. In the case of the first,
its value must be a dictionary.
"""
# We set a 20% of the lifetime randomly, so that if we have thousands of jobs
# starting at the same time, all the caches will not end at the same time.
randomLifeTimeBias = 0.2 * random.random()
self.log = gLogger.getSubLogger( self.__class__.__name__ )
self.__lifeTime = int( lifeTime * ( 1 + randomLifeTimeBias ) )
self.__updateFunc = updateFunc
# The records returned from the cache must be valid at least 10 seconds.
self.__validSeconds = 10
# Cache
self.__cache = DictCache()
self.__cacheLock = LockRing()
self.__cacheLock.getLock( self.__class__.__name__ )
#.............................................................................
# internal cache object getter
def cacheKeys( self ):
"""
Cache keys getter
:returns: list with valid keys on the cache
"""
return self.__cache.getKeys( validSeconds = self.__validSeconds )
#.............................................................................
# acquire / release Locks
def acquireLock( self ):
"""
Acquires Cache lock
"""
self.__cacheLock.acquire( self.__class__.__name__ )
def releaseLock( self ):
"""
Releases Cache lock
"""
self.__cacheLock.release( self.__class__.__name__)
#.............................................................................
# Cache getters
def get( self, cacheKeys ):
"""
Gets values for cacheKeys given, if all are found ( present on the cache and
valid ), returns S_OK with the results. If any is not neither present not
valid, returns S_ERROR.
:Parameters:
**cacheKeys** - `list`
list of keys to be extracted from the cache
:return: S_OK | S_ERROR
"""
result = {}
for cacheKey in cacheKeys:
cacheRow = self.__cache.get( cacheKey, validSeconds = self.__validSeconds )
if not cacheRow:
self.log.error( str( cacheKey ) )
return S_ERROR( 'Cannot get %s' % str( cacheKey ) )
result.update( { cacheKey : cacheRow } )
return S_OK( result )
#.............................................................................
# Cache refreshers
def refreshCache( self ):
"""
Purges the cache and gets fresh data from the update function.
#.........这里部分代码省略.........
示例4: __init__
# 需要导入模块: from DIRAC.Core.Utilities.LockRing import LockRing [as 别名]
# 或者: from DIRAC.Core.Utilities.LockRing.LockRing import getLock [as 别名]
class DictCache:
def __init__( self, deleteFunction = False ):
"""
Initialize the dict cache.
If a delete function is specified it will be invoked when deleting a cached object
"""
self.__lock = LockRing()
self.__lock.getLock( self.__class__.__name__, recursive = True )
self.__cache = {}
self.__deleteFunction = deleteFunction
def exists( self, cKey, validSeconds = 0 ):
"""
Returns True/False if the key exists for the given number of seconds
Arguments:
- cKey : identification key of the record
- validSeconds : The amount of seconds the key has to be valid for
"""
self.__lock.acquire( self.__class__.__name__ )
try:
#Is the key in the cache?
if cKey in self.__cache:
expTime = self.__cache[ cKey ][ 'expirationTime' ]
#If it's valid return True!
if expTime > datetime.datetime.now() + datetime.timedelta( seconds = validSeconds ):
return True
else:
#Delete expired
self.delete( cKey )
return False
finally:
self.__lock.release( self.__class__.__name__ )
def delete( self, cKey ):
"""
Delete a key from the cache
Arguments:
- cKey : identification key of the record
"""
self.__lock.acquire( self.__class__.__name__ )
try:
if cKey not in self.__cache:
return
if self.__deleteFunction:
self.__deleteFunction( self.__cache[ cKey ][ 'value' ] )
del( self.__cache[ cKey ] )
finally:
self.__lock.release( self.__class__.__name__ )
def add( self, cKey, validSeconds, value = None ):
"""
Add a record to the cache
Arguments:
- cKey : identification key of the record
- validSeconds : valid seconds of this record
- value : value of the record
"""
if max( 0, validSeconds ) == 0:
return
self.__lock.acquire( self.__class__.__name__ )
try:
vD = { 'expirationTime' : datetime.datetime.now() + datetime.timedelta( seconds = validSeconds ),
'value' : value }
self.__cache[ cKey ] = vD
finally:
self.__lock.release( self.__class__.__name__ )
def get( self, cKey, validSeconds = 0 ):
"""
Get a record from the cache
Arguments:
- cKey : identification key of the record
- validSeconds : The amount of seconds the key has to be valid for
"""
self.__lock.acquire( self.__class__.__name__ )
try:
#Is the key in the cache?
if cKey in self.__cache:
expTime = self.__cache[ cKey ][ 'expirationTime' ]
#If it's valid return True!
if expTime > datetime.datetime.now() + datetime.timedelta( seconds = validSeconds ):
return self.__cache[ cKey ][ 'value' ]
else:
#Delete expired
self.delete( cKey )
return False
finally:
self.__lock.release( self.__class__.__name__ )
def showContentsInString( self ):
"""
Return a human readable string to represent the contents
"""
self.__lock.acquire( self.__class__.__name__ )
try:
data = []
for cKey in self.__cache:
#.........这里部分代码省略.........