当前位置: 首页>>代码示例>>Python>>正文


Python LockRing.acquire方法代码示例

本文整理汇总了Python中DIRAC.Core.Utilities.LockRing.LockRing.acquire方法的典型用法代码示例。如果您正苦于以下问题:Python LockRing.acquire方法的具体用法?Python LockRing.acquire怎么用?Python LockRing.acquire使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DIRAC.Core.Utilities.LockRing.LockRing的用法示例。


在下文中一共展示了LockRing.acquire方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: wrapped_fcn

# 需要导入模块: from DIRAC.Core.Utilities.LockRing import LockRing [as 别名]
# 或者: from DIRAC.Core.Utilities.LockRing.LockRing import acquire [as 别名]
  def wrapped_fcn( *args, **kwargs ):

    # Get the lock and acquire it
    executionLock = LockRing().getLock( '_UseUserProxy_', recursive = True )
    executionLock.acquire()

    # Check if the caller is executing with the host certificate
    useServerCertificate = gConfig.useServerCertificate()
    if useServerCertificate:
      gConfigurationData.setOptionInCFG( '/DIRAC/Security/UseServerCertificate', 'false' )

    try:
      return fcn( *args, **kwargs )
    except Exception as lException:  # pylint: disable=broad-except
      value = ','.join( [str( arg ) for arg in lException.args] )
      exceptType = lException.__class__.__name__
      return S_ERROR( "Exception - %s: %s" % ( exceptType, value ) )
    finally:
      # Restore the default host certificate usage if necessary
      if useServerCertificate:
        gConfigurationData.setOptionInCFG( '/DIRAC/Security/UseServerCertificate', 'true' )
      # release the lock
      executionLock.release()
开发者ID:DIRACGrid,项目名称:DIRAC,代码行数:25,代码来源:Proxy.py

示例2: _putProxy

# 需要导入模块: from DIRAC.Core.Utilities.LockRing import LockRing [as 别名]
# 或者: from DIRAC.Core.Utilities.LockRing.LockRing import acquire [as 别名]
def _putProxy(userDN=None, userName=None, userGroup=None, vomsFlag=None, proxyFilePath=None, executionLockFlag=False):
  """Download proxy, place in a file and populate X509_USER_PROXY environment variable.

  Parameters like `userProxy` or `executeWithUserProxy`.
  :returns: Tuple of originalUserProxy, useServerCertificate, executionLock
  """
  # Setup user proxy
  if userDN:
    userDNs = [userDN]
  else:
    result = getDNForUsername(userName)
    if not result['OK']:
      return result
    userDNs = result['Value']  # a same user may have more than one DN

  vomsAttr = ''
  if vomsFlag:
    vomsAttr = getVOMSAttributeForGroup(userGroup)

  result = getProxy(userDNs, userGroup, vomsAttr, proxyFilePath)

  if not result['OK']:
    return result

  executionLock = LockRing().getLock('_UseUserProxy_', recursive=True) if executionLockFlag else None
  if executionLockFlag:
    executionLock.acquire()

  os.environ['X509_USER_PROXY'], originalUserProxy = result['Value'], os.environ.get('X509_USER_PROXY')

  # Check if the caller is executing with the host certificate
  useServerCertificate = gConfig.useServerCertificate()
  if useServerCertificate:
    gConfigurationData.setOptionInCFG('/DIRAC/Security/UseServerCertificate', 'false')

  return S_OK((originalUserProxy, useServerCertificate, executionLock))
开发者ID:DIRACGrid,项目名称:DIRAC,代码行数:38,代码来源:Proxy.py

示例3: MessageForwarder

# 需要导入模块: from DIRAC.Core.Utilities.LockRing import LockRing [as 别名]
# 或者: from DIRAC.Core.Utilities.LockRing.LockRing import acquire [as 别名]
class MessageForwarder(object):

  def __init__( self, msgBroker ):
    self.__inOutLock = LockRing().getLock()
    self.__msgBroker = msgBroker
    self.__byClient = {}
    self.__srvToCliTrid = {}

  def addClient( self, cliTrid, destination, clientInitParams, connectParams ):
    if cliTrid in self.__byClient:
      gLogger.fatal( "Trid is duplicated!! this shouldn't happen" )
      return
    msgClient = MessageClient( destination, **clientInitParams )
    msgClient.subscribeToDisconnect( self.__srvDisconnect )
    msgClient.subscribeToAllMessages( self.msgFromSrv )
    msgClient.setUniqueName( connectParams[0] )
    result = msgClient.connect( **connectParams[1] )
    if not result[ 'OK' ]:
      return result
    self.__inOutLock.acquire()
    try:
      self.__byClient[ cliTrid ] = { 'srvEnd' : msgClient,
                                     'srvTrid' : msgClient.getTrid(),
                                     'srvName' : destination }
      self.__srvToCliTrid[ msgClient.getTrid() ] = cliTrid
    finally:
      self.__inOutLock.release()
    return result

  def __srvDisconnect( self, srvEndCli ):
    try:
      cliTrid = self.__srvToCliTrid[ srvEndCli.getTrid() ]
    except IndexError:
      gLogger.exception( "This shouldn't happen!" )
    gLogger.info( "Service %s disconnected messaging connection" % self.__byClient[ cliTrid ][ 'srvName' ] )
    self.__msgBroker.removeTransport( cliTrid )
    self.__removeClient( cliTrid )

  def cliDisconnect( self, cliTrid ):
    if cliTrid not in self.__byClient:
      gLogger.fatal( "This shouldn't happen!" )
      return
    gLogger.info( "Client to %s disconnected messaging connection" % self.__byClient[ cliTrid ][ 'srvName' ] )
    self.__byClient[ cliTrid ][ 'srvEnd' ].disconnect()
    self.__removeClient( cliTrid )

  def __removeClient( self, cliTrid ):
    self.__inOutLock.acquire()
    try:
      try:
        srvTrid = self.__byClient[ cliTrid ][ 'srvTrid' ]
        self.__byClient.pop( cliTrid )
        self.__srvToCliTrid.pop( srvTrid )
      except Exception as e:
        gLogger.exception( "This shouldn't happen!" )
    finally:
      self.__inOutLock.release()

  def msgFromClient( self, cliTrid, msgObj ):
    gLogger.info( "Message %s to %s service" % ( msgObj.getName(), self.__byClient[ cliTrid ][ 'srvName' ] ) )
    result = self.__byClient[ cliTrid ][ 'srvEnd' ].sendMessage( msgObj )
    return result

  def msgFromSrv( self, srvEndCli, msgObj ):
    try:
      cliTrid = self.__srvToCliTrid[ srvEndCli.getTrid() ]
    except:
      gLogger.exception( "This shouldn't happen" )
      return S_ERROR( "MsgFromSrv -> Mismatched srv2cli trid" )
    gLogger.info( "Message %s from %s service" % ( msgObj.getName(), self.__byClient[ cliTrid ][ 'srvName' ] ) )
    return self.__msgBroker.sendMessage( cliTrid, msgObj )
开发者ID:DIRACGrid,项目名称:DIRAC,代码行数:73,代码来源:GatewayService.py

示例4: Cache

# 需要导入模块: from DIRAC.Core.Utilities.LockRing import LockRing [as 别名]
# 或者: from DIRAC.Core.Utilities.LockRing.LockRing import acquire [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.
#.........这里部分代码省略.........
开发者ID:IgorPelevanyuk,项目名称:DIRAC,代码行数:103,代码来源:RSSCacheNoThread.py

示例5: __init__

# 需要导入模块: from DIRAC.Core.Utilities.LockRing import LockRing [as 别名]
# 或者: from DIRAC.Core.Utilities.LockRing.LockRing import acquire [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:
#.........这里部分代码省略.........
开发者ID:kanwx,项目名称:DIRAC,代码行数:103,代码来源:DictCache.py


注:本文中的DIRAC.Core.Utilities.LockRing.LockRing.acquire方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。