• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python threading.RLock类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中threading.RLock的典型用法代码示例。如果您正苦于以下问题:Python RLock类的具体用法?Python RLock怎么用?Python RLock使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



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

示例1: Framer

class Framer(Packer):

  HEADER="!4s4B"

  def __init__(self, sock):
    self.sock = sock
    self.sock_lock = RLock()
    self.tx_buf = ""
    self.rx_buf = ""
    self.security_layer_tx = None
    self.security_layer_rx = None
    self.maxbufsize = 65535

  def aborted(self):
    return False

  def write(self, buf):
    self.tx_buf += buf

  def flush(self):
    self.sock_lock.acquire()
    try:
      if self.security_layer_tx:
        try:
          cipher_buf = self.security_layer_tx.encode(self.tx_buf)
        except SASLError, e:
          raise Closed(str(e))
        self._write(cipher_buf)
      else:
开发者ID:ChugR,项目名称:qpid-python,代码行数:29,代码来源:framer.py


示例2: ThreadSafeFSM

class ThreadSafeFSM(InstrumentFSM):
    """
    A FSM class that provides thread locking in on_event to
    prevent simultaneous thread reentry.
    """

    def __init__(self, states, events, enter_event, exit_event):
        """
        """
        super(ThreadSafeFSM, self).__init__(states, events, enter_event, exit_event)
        self._lock = RLock()

    def on_event(self, event, *args, **kwargs):
        """
        """

        self._lock.acquire(True)
        ex = None

        try:
            result = super(ThreadSafeFSM, self).on_event(event, *args, **kwargs)

        except Exception as ex:
            result = None
            log.error("Unhandled Exception")
            log.exception(ex)

        finally:
            self._lock.release()

        if ex:
            raise ex

        return result
开发者ID:kehunt06,项目名称:mi-instrument,代码行数:34,代码来源:instrument_fsm.py


示例3: SingleIndexCache

class SingleIndexCache(object):
    def __init__(self):
        self.lock = RLock()
        self.cached_vals = []

    def __enter__(self):
        self.lock.acquire()
        return self

    def fill(self, values):
        self.cached_vals = values

    def wipe(self):
        self.cached_vals = []

    def get_index(self, real_funct, ind_name, **kwargs):
        kwargs.setdefault("max_results", 999999)
        if not self.cached_vals:
            recvs = real_funct(ind_name, **kwargs).results
            self.fill(recvs)

    def filter(self, startkey, endkey, max_results=1):
        c = self.cached_vals
        for (curr_val, obj_key) in c:
            if max_results == 0:
                break
            if curr_val >= startkey:
                if curr_val <= endkey:
                    max_results -= 1
                    yield (curr_val, obj_key)
                else:
                    break

    def __exit__(self, *args, **kwargs):
        self.lock.release()
开发者ID:loles,项目名称:solar,代码行数:35,代码来源:model.py


示例4: XBeeTransparentListener

class XBeeTransparentListener(Thread):

    def __init__(self, xbee_serial):
        super().__init__()
        self.xbser = xbee_serial
        self.daemon = True
        self.stopped = False
        self.pause = RLock()

    def run(self):
        while not self.stopped and self.xbser.is_open:
            with self.pause:
                try:
                    line = self.xbser.readline()
                    if line:
                        print('>', line.strip())
                except Exception as ex:
                    print(str(ex))

    def stop(self):
        self.stopped = True

    def pause(self):
        self.pause.acquire()

    def unpause(self):
        self.pause.release()
开发者ID:ronsmith,项目名称:xbee_transp,代码行数:27,代码来源:transparent.py


示例5: __init__

class SimpleCSVWriter:
    def __init__(self, filename, fields=None):
        self.filename = filename
        self.lock = RLock()
        self.isFirstRow = True
        self.fields = fields
        
    def writerow(self, d):
        self.lock.acquire()
        fields = self.fields if self.fields is not None else d.keys()
        if self.isFirstRow:
            # dump fields
            f = open(self.filename , "w")
            writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_ALL)
            row = [k for k in fields]
            writer.writerow(row)
            f.close()
            self.isFirstRow = False
        # dump object
        row = [d.get(k,'') for k in fields]
        f = open(self.filename , "a")
        writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_ALL)
        writer.writerow(row)
        f.close()
        self.lock.release()
开发者ID:maxpmaxp,项目名称:sterch.scrapingtools,代码行数:25,代码来源:writer.py


示例6: __init__

    def __init__(self):
        Thread.__init__(self)

        self.positions = list()
        self.stopped = False

        self.startTime = time.time()

        self.timer = 0
        self.counter = 0
        self.total_uavs = 0  # Total number of UAVs in the air
        self.steps_counter = 0

        self.turn_kill_counter = 0

        self.writeState = Event()
        self.writeCounter = 0
        self.readState = Event()
        self.readCounter = 0

        self.lock = RLock()
        self.lock2 = RLock()
        try:
            self.readState.set()
            logging.info('Read state set.')
        except:
            logging.error('Read state not set.')
开发者ID:CptMacHammer,项目名称:3D_CA,代码行数:27,代码来源:centralizedComm.py


示例7: __init__

 def __init__(self, req, proxy, logger, task, exit_check=None, ignored_errors=[]):
     Thread.__init__(self, name = "monitor%s" % task.guid)
     Thread.setDaemon(self, True)
     # the count of votes per error code
     self.vote_result = {}
     # the error code to be ignored
     self.vote_cleared = set().union(ignored_errors)
     self.thread_last_seen = {}
     self.dctlock = RLock()
     self.votelock = RLock()
     self.thread_ref = {}
     self.thread_zombie = set()
     # HttpReq instance
     self.req = req
     # proxy.Pool instance
     self.proxy = proxy
     self.logger = logger
     self.task = task
     self._exit = exit_check if exit_check else lambda x: False
     self._cleaning_up = False
     if os.name == "nt":
         self.set_title = lambda s:os.system("TITLE %s" % (
             s if PY3K else s.encode(CODEPAGE, 'replace')))
     elif os.name == 'posix':
         import sys
         self.set_title = lambda s:sys.stdout.write("\033]2;%s\007" % (
             s if PY3K else s.encode(CODEPAGE, 'replace')))
开发者ID:fffonion,项目名称:xeHentai,代码行数:27,代码来源:worker.py


示例8: CacheDict

class CacheDict(dict):
  
  _cacheTimeout = 0
  _accesslock   = None

  def __init__(self, timeout):
    self._cacheTimeout = timeout
    self._accesslock   = RLock()

  def get(self, key):
    return self[key]['value']

  def cache(self, key, value):
    self[key] = { 'time' : time.time(), 'value' : value }
    
  def isObsolete(self, key):
    return (not self.has_key(key) or
            time.time() - self[key]['time'] > self._cacheTimeout)
    
  def invalidate(self, key):
    if self.has_key(key):
      return self.pop(key)['value']

  def acquire(self):
    self._accesslock.acquire()

  def release(self):
    self._accesslock.release()
开发者ID:vienin,项目名称:python-ufo,代码行数:28,代码来源:utils.py


示例9: DataSignaler

class DataSignaler(object):
    def __init__(self, name, pruneFunc, data):
        super(DataSignaler, self).__init__()

        assert isinstance(data,TreeFunctioned)

        self.data = data
        self.event_signaler = EventSignaler(key=name)

        if pruneFunc is not None:
            assert callable(pruneFunc)

        self.prune_func = pruneFunc
        self._lock = RLock()

    def add(self, value):
        self._lock.acquire()

        try:
            self.data.addToTreeByFunction(value)
        finally:
            self._lock.release()

        data = {self.event_signaler.key : {'data': self.data}}
        self.event_signaler.signalEvent(data)

    def prune(self):
        if self.prune_func is not None:
            return criticalSection(self._lock, lambda: self.prune_func(dataStructure=self.data))

    def inByFunction(self, value, hashFuncList=None, depth=0):
       return criticalSection(self._lock, lambda: self.data.inByFunction(value, hashFuncList, depth))

    def getOriginalByFunction(self, value, hashFuncList=None, depth=0):
        return criticalSection(self._lock, lambda: self.data.getOriginalByFunction(value, hashFuncList, depth))
开发者ID:vivek8943,项目名称:GeoTweetSearch,代码行数:35,代码来源:data_core.py


示例10: Database

class Database(object):
    UNICODE_TRANSLATE = {ord(u'ö'): u'o', ord(u'ä'): u'a', ord(u'ü'): u'u'}

    def __init__(self, filename):
        self.database_file = filename
        self.channel_map = {}
        self._db = None
        self.lock = RLock()

    def close(self):
        self.commit()

        cur = self.cursor(lock=True)
        log('Optimizing database for faster startup.', sender=self)
        cur.execute("VACUUM")
        cur.close()
        self.lock.release()

        self._db.close()
        self._db = None

    def log(self, message, *args, **kwargs):
        if False:
            try:
                message = message % args
                log('%s', message, sender=self)
            except TypeError, e:
                log('Exception in log(): %s: %s', e, message, sender=self)
开发者ID:timabell,项目名称:gpodder,代码行数:28,代码来源:dbsqlite.py


示例11: __init__

 def __init__(self, cache, store, logger, max_writer_threads=30):
     '''Max. throughput for files < 1MB is max_writer_threads * 100KB per second.
     :param max_writer_threads: Max. number of writer threads to use.
     '''
     self.logger = logger
     self.stats = WorkerStats()
     self.cache = cache
     self.store = store
     self.max_writer_threads = max_writer_threads
     self.WRITE_TIMELIMIT = 60*60*2 #2h
     self.lock = RLock()
     self.protect_cache_from_write_access = RLock() #could also be normal lock
     self.oldest_modified_date = {} #keep track of modified date of a cache entry when it is first enqueued for upload. Their contents might change during upload.
     self.removers = []
     self.writers = []
     self.readers = []
     self._stop = False
     self.thread = None
     self.last_reconnect = time.time()
     self._heartbeat = time.time()
     #used for waiting when quota errors occur
     self.skip_starting_new_writers_for_next_x_cycles = 0
     self.do_profiling = False
     self.upload_process_pool = WriteWorkerProcesses(store, logger)
     self.logger.info("initialized StoreSyncThread")
开发者ID:PavilionVI,项目名称:CloudFusion,代码行数:25,代码来源:store_sync_thread.py


示例12: close_dynamic_queue

    def close_dynamic_queue(self, dynamic_queue_name):
        if self._disconnecting:
            self.logger.info("Connection factory disconnecting, aborting close_dynamic_queue")
            return
        else:
            self.logger.log(TRACE1, "close_dynamic_queue -> not disconnecting")

        if not self._is_connected:
            # If we're not connected then all dynamic queues had been already closed.
            self.logger.log(TRACE1, "close_dynamic_queue -> _is_connected1 %s" % self._is_connected)
            return
        else:
            self.logger.log(TRACE1, "close_dynamic_queue -> _is_connected2 %s" % self._is_connected)
            lock = RLock()
            lock.acquire()
            try:
                dynamic_queue = self._open_dynamic_queues_cache[dynamic_queue_name]
                dynamic_queue.close()

                self._open_dynamic_queues_cache.pop(dynamic_queue_name, None)
                self._open_send_queues_cache.pop(dynamic_queue_name, None)
                self._open_receive_queues_cache.pop(dynamic_queue_name, None)

                self.logger.log(TRACE1, "Successfully closed a dynamic queue [%s]" % (
                    dynamic_queue_name))

            finally:
                lock.release()
开发者ID:Cophy08,项目名称:spring-python,代码行数:28,代码来源:factory.py


示例13: open_dynamic_queue

    def open_dynamic_queue(self):
        if self._disconnecting:
            self.logger.info("Connection factory disconnecting, aborting open_dynamic_queue")
            return
        else:
            self.logger.log(TRACE1, "open_dynamic_queue -> not disconnecting")

        if not self._is_connected:
            self.logger.log(TRACE1, "open_dynamic_queue -> _is_connected1 %s" % self._is_connected)
            self._connect()
            self.logger.log(TRACE1, "open_dynamic_queue -> _is_connected2 %s" % self._is_connected)

        dynamic_queue = self.mq.Queue(self.mgr, self.dynamic_queue_template,
            self.CMQC.MQOO_INPUT_SHARED)

        # A bit hackish, but there's no other way to get its name.
        dynamic_queue_name = dynamic_queue._Queue__qDesc.ObjectName.strip()

        lock = RLock()
        lock.acquire()
        try:
            self._open_dynamic_queues_cache[dynamic_queue_name] = dynamic_queue
        finally:
            lock.release()

        self.logger.log(TRACE1, "Successfully created a dynamic queue, descriptor [%s]" % (
            dynamic_queue._Queue__qDesc))

        return dynamic_queue_name
开发者ID:Cophy08,项目名称:spring-python,代码行数:29,代码来源:factory.py


示例14: XBeeTransparentListener

class XBeeTransparentListener(Thread):

    def __init__(self, on_received=None):
        super().__init__()
        self.xbser = None
        self.on_received = on_received
        self.daemon = True
        self.stopped = False
        self.pause = RLock()

    def run(self):
        while not self.stopped and self.xbser.is_open:
            with self.pause:
                try:
                    line = self.xbser.readline()
                    if line:
                        self.received(line)
                except Exception as ex:
                    print(str(ex))

    def received(self, line):
        """Subclasses may override this method, or provide a callback function when instance is created"""
        if self.on_received:
            self.on_received(line)
        else:
            print('[XBee]', line.strip())

    def stop(self):
        self.stopped = True

    def pause(self):
        self.pause.acquire()

    def unpause(self):
        self.pause.release()
开发者ID:ronsmith,项目名称:hazel,代码行数:35,代码来源:transparent.py


示例15: VM_Pool

class VM_Pool(object):

    def __init__(self, vm_map):
        self.proc_mgr = proc_mgmt.ProcMgr()
        self.vm_map = vm_map
        self.vm_rdy = {}
        self.init_map()
        self.pool_gate = RLock()
        
    def acquire(self, *names):
        self.pool_gate.acquire()
        for name in names:
            if self.vm_rdy.get(name):
                self.vm_rdy[name] = False
                return self.vm_map.get(name)
        self.pool_gate.release()
        return None
    
    def release(self, name):
        self.vm_rdy[name] = True
      
    def init_map(self):
        for name, vm_obj in self.vm_map.items():
            self.vm_rdy[name] = True

    def __str__(self):
        string = 'Pool:'
        for vm in self.vm_map.keys():
            string += vm + ": " + str(self.vm_rdy.get(vm)) + ", "
        return string
开发者ID:toejamhoney,项目名称:voodo,代码行数:30,代码来源:vm_mgmt.py


示例16: ScrollableHandler

class ScrollableHandler(StreamHandlerNoLF):
    """ ScrollableHandler is a StreamHandler that specially handles scrolling (log
    messages at the SCROLL level). It allows you to temporarily interrupt the constant
    scroll with other log messages of different levels (printed at the top of the scroll
    area) """

    # the SCROLL level (a class var)
    LOGFILE = 11
    SCROLL = 12
    SHUTDOWN = 13
    NOLOGFILE = 14
    
    def __init__(self, *args, **kwargs):
        self.scrollLock = RLock()
        self.scrollFlag = False
        StreamHandlerNoLF.__init__(self, *args, **kwargs)

    def handle(self, record):
        """ The 'scroll' level is a constant scroll that can be interrupted. This interruption is
        done via prepending text to the scroll area """
        rv = self.filter(record)
        if rv:

            if record.levelno == ScrollableHandler.SCROLL:
                self.emitSynchronized(record)
            elif record.levelno == ScrollableHandler.SHUTDOWN:
                record.msg = '\n\n\n%s\n' % record.msg
                self.emitSynchronized(record)
            else:
                self.scrollLock.acquire()
                # If scroll is on, interrupt scroll
                if ScrollableHandler.scrollFlag:
                    self.scrollHeader(record)
                else:
                    # otherwise if scroll isn't on, just log the message normally
                    self.emitSynchronized(record)
                self.scrollLock.release()
                            
        return rv

    def emitSynchronized(self, record):
        """ Write a log message atomically. Normal python logging Handler behavior """
        self.acquire()
        try:
            self.emit(record)
        finally:
            self.release()

    def scrollHeader(self, record):
        """ Print a log message so that the user can see it during a SCROLL """
        msg = self.format(record).rstrip() # Scroller appends newline for us
        from twisted.internet import reactor
        if inMainThread():
            # FIXME: scrollBegin() should really be creating the scroller instance
            # FIXME: no unicode crap from normal python log emit
            Hellanzb.scroller.scrollHeader(msg)
        else:
            reactor.callFromThread(Hellanzb.scroller.scrollHeader, msg)
开发者ID:myusuf3,项目名称:hellanzb,代码行数:58,代码来源:Logging.py


示例17: Main

class Main(HTTPServlet):
    registerShutdown = 1
    
    def __init__(self):
        HTTPServlet.__init__(self)
        
        self.blogDirectory = WeblogDirectory("../blogs.xml")    
        self.mutex = RLock()
             
    def awake(self, transaction):
        # Register our shutdown handler if it hasn't already been done. This is to
        # make sure the databases are properly closed when the system is shutdown.
        self.mutex.acquire()
        try:        
            if (Main.registerShutdown == 1):
                transaction.application().addShutDownHandler(self.blogDirectory.shutdown)
                Main.registerShutdown = 0
        finally:
            self.mutex.release()
            
    def respondToGet(self, transaction):
        request = transaction.request()
        response = transaction.response()
        
        pathInfo = request.extraURLPath() 

        try:
            (blog, pathInfo) = self._parsePathInfo(pathInfo)
            weblog = self.blogDirectory.getBlog(blog)
        
            try:
                stylesheet = request.field('t', "")
                # Extra optional argument that can be passed to the stylesheet
                arg = request.field('a', "")
            
                # Content query that can be applied as a final step to extract
                # something from the rendered content
                contentQuery = request.field('c', "")
            
                result = weblog.handleRequest(pathInfo, stylesheet, arg, contentQuery)
            
                # Determine the content-type for the result
                if (result.startswith("<?xml")):                             
                    contentType = "text/xml"         
                elif (result.startswith("<html")):
                    contentType = "text/html"
                else:
                    contentType = "text/plain"
                #print result
                
                response.setStatus(200, 'OK')
                response.setHeader('Content-type', contentType)
                response.setHeader('Content-length', str(len(result)))
                response.write(result)
            except NotFoundError:
                response.setStatus(404, 'Not Found')
        except KeyError, IndexError:
            response.setStatus(404, 'Weblog Not Found')
开发者ID:kstaken,项目名称:Syncato,代码行数:58,代码来源:Main-orig.py


示例18: __init__

class Chopstick:
    def __init__(self, name):
        self.lock = RLock()
        self.name = name

    def pickUp(self):
        return self.lock.acquire(blocking=False)

    def putDown(self):
        self.lock.release()
开发者ID:hydersm,项目名称:CTCI,代码行数:10,代码来源:q_16_3.py


示例19: exit

 def exit(self):
     global command_output_thread
     print("Exiting thread {0}...".format(self))
     lock = RLock()
     lock.acquire()
     self.EXIT_FLAG = True
     pgid = self.get_pgid(True)
     if pgid is not None:
         call(["ionice", "-c", "0", "-P", str(self.get_pgid(True))])
     lock.release()
开发者ID:Saren-Arterius,项目名称:IO-Verynice,代码行数:10,代码来源:main.py


示例20: _threadCallback

 def _threadCallback(self,thread):
   """
   A callback function which gets called when a code thread terminates.
   """
   lock = RLock()
   lock.acquire()
   if thread.failed():
     self._exceptions[thread._id] = thread.exceptionInfo()
     self._tracebacks[thread._id] = thread.tracebackInfo()
   lock.release()
开发者ID:zennro,项目名称:pyview,代码行数:10,代码来源:coderunner.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python threading.Semaphore类代码示例发布时间:2022-05-27
下一篇:
Python threading.Lock类代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap