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


Python threading.Condition类代码示例

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


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

示例1: TestBackend

class TestBackend(Backend):
    def __init__(self, queue):
        self.lock = Condition()
        queue._set_backend(self)

        # Statistics
        self.notifies = 0

    def start(self):
        pass

    def stop(self):
        pass

    def start_feedback(self):
        pass

    def queue_lock(self):
        return self.lock

    def queue_notify(self):
        self.notifies += 1

        self.lock.notify_all()

    def sleep(self):
        pass
开发者ID:bbits,项目名称:apns-worker,代码行数:27,代码来源:test_queue.py

示例2: __init__

 def __init__(self):
     '''Init.'''
     gobject.GObject.__init__(self)
     
     # set condition lock.
     self.__condition = Condition()
     
     # set datebase operation lock.
     self.__db_operation_lock = Condition()
     
     # songs
     self.__hiddens = set()        
     self.__song_types_capability = {}
     self.__songs = {}
     self.__song_types = []
     self.__songs_by_type = {}
     
     # playlist
     self.__playlists = {}
     self.__playlist_types = []
     
     # init constant
     self.__is_loaded = False
     self.__force_check = False
     self.__save_song_type = ["local", "cue", "unknown"]
     self.__dirty = False
     
     # Init queued signal.
     self.__reset_queued_signal()
开发者ID:WilliamRen,项目名称:deepin-music-player,代码行数:29,代码来源:library.py

示例3: SimpleBlockingQueue

class SimpleBlockingQueue(object):
    def __init__(self):
        self.items = deque()
        self.condition = Condition()
        self.interrupted = False

    def put(self, item):
        with self.condition:
            self.items.pushleft(item)

    def take(self, block=False):
        with self.condition:
            if block:
                while not self.items:
                    self.condition.wait()
                    if self.interrupted:
                        self.interrupted = False
                        return None
            elif not self.items:
                return None

            return self.items.popright()

    def interrupt(self):
        with self.condition:
            self.interrupted = True
            self.condition.notifyAll()
开发者ID:RecruiterBill,项目名称:zephyr-mobile,代码行数:27,代码来源:test_zephyr.py

示例4: Future

class Future(object):
    def __init__(self, func, *args, **kwargs):
        self.__done = False
        self.__result = None
        self.__cond = Condition()
        self.__func = func
        self.__args = args
        self.__kwargs = kwargs
        self.__except = None

    def __call__(self):
        with self.__cond:
            try:
                self.__result = self.__func(*self.__args, **self.__kwargs)
            except:
                self.__result = None
                self.__except = sys.exc_info()
            self.__done = True
            self.__cond.notify()

    def result(self):
        with self.__cond:
            while not self.__done:
                self.__cond.wait()
        if self.__except:
            exc = self.__except
            reraise(exc[0], exc[1], exc[2])
        result = self.__result
        return copy.deepcopy(result)
开发者ID:sevensky,项目名称:MyST3Data,代码行数:29,代码来源:Common.py

示例5: SendBuffer

class SendBuffer(object):

    def __init__(self, max_size):
        self.size = 0
        self.frontbuffer = ''
        self.full = Condition()
        self.backbuffer = StringIO()
        self.max_size = max_size

    def __len__(self):
        return len(self.frontbuffer) + self.size

    def write(self, data):
        dlen = len(data)
        with self.full:
            while self.size + dlen > self.max_size and dlen < self.max_size:
                # if a single write is bigger than the buffer, swallow hard.
                # No amount of waitng will make it fit.
                self.full.wait()
            self.backbuffer.write(data)
            self.size += dlen

    def to_sock(self, sock):
        if not self.frontbuffer:
            with self.full:
                buf, self.backbuffer = self.backbuffer, StringIO()
                self.size = 0
                self.full.notify_all()
            self.frontbuffer = buf.getvalue()

        written = sock.send(self.frontbuffer)
        self.frontbuffer = self.frontbuffer[written:]
开发者ID:pepijndevos,项目名称:pypredis,代码行数:32,代码来源:sendbuffer.py

示例6: __init__

    def __init__(self, client):
        self.prefix = "auau:"
        self.mc = client

        self.random = Random()
        self.random.seed(os.urandom(128))
        self.random.jumpahead(os.getpid())
        # thread exit flag
        self.exit_flag = [False]

        from threading import Thread
        from threading import Condition

        # asynchronous deflation thread
        self.def_cv = Condition()
        self.def_que = []
        self.def_thread = Thread(
            target=lambda: self.async_work(self.def_cv, self.def_que, self.exit_flag, lambda x: self.deflate(x))
        )
        self.def_thread.setDaemon(True)
        self.def_thread.start()

        # asynchronous deletion thread
        self.del_cv = Condition()
        self.del_que = []
        self.del_thread = Thread(
            target=lambda: self.async_work(self.del_cv, self.del_que, self.exit_flag, lambda x: self.mc.delete(x))
        )
        self.del_thread.setDaemon(True)
        self.del_thread.start()
开发者ID:kumagi,项目名称:kvtx2,代码行数:30,代码来源:__init__.py

示例7: IOManager

class IOManager(Thread):

    def __init__(self,so):
        Thread.__init__(self)
        self.mutexIO = Condition()
        self.listIO = []
        self.handlerIO = so.handlerIO
    
    def process(self):
        for irq in self.listIO:
            print("Ejecuto IOManager")
            self.handlerIO.handle(irq)
        self.removeIOFromListIO()
        
    def run(self):
        while(True):
            with self.mutexIO:
                self.mutexIO.wait()
                self.process()
    
    def removeIOFromListIO(self):
        for reqIO in self.listIO:
            self.listIO.remove(reqIO)
              
    def add(self,iOReq):
        self.listIO.append(iOReq)
开发者ID:Bequita,项目名称:EmuladorS.O.,代码行数:26,代码来源:IOManager.py

示例8: Worker

class Worker(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.lock = Lock()
        self.cond = Condition(self.lock)
        self.stopped = False
        self.queue = []

    def run(self):
        while True:
            job = None
            with self.cond:
                if self.stopped:
                    return
                if not self.queue:
                    self.cond.wait()
                else:
                    job, params = self.queue.pop(0)
            if not job:
                continue
            self.execute(job, params)

    def execute(self, job, params):
        try:
            func, args = job(*params)
            # The async function may decide to NOT update
            # the UI:
            if func:
                gobject.idle_add(func, *args)
        except Exception, e:
            print "Warning:", e
开发者ID:pablojorge,项目名称:gtk-viewer,代码行数:31,代码来源:threads.py

示例9: __init__

 def __init__(self, num_photos=10, sources=[], pool_dir=cache_dir):
     Thread.__init__(self)
     
     self.num_photos = num_photos
     self.sources = sources
     self.dir = pool_dir
     
     # Make sure cache dir exists
     if not os.path.exists(self.dir):
         os.mkdir(self.dir)
     
     # Clean cache directory
     self.clean_cache()
     
     # Load cached photos
     self.photos = os.listdir(self.dir)
     
     # Delete queue
     self.trash = []
     
     # Condition when a new photo is added
     self.added = Condition()
     
     # Condition when a photo is removed
     self.removed = Condition()
     
     # Event for stopping the pool
     self._stop = Event()
开发者ID:joh,项目名称:Flickrsaver,代码行数:28,代码来源:flickrsaver.py

示例10: __init__

 def __init__(self, algorithm, interpreter, change_listener, debug=False,
              progress_listener=None):
     if not isinstance(algorithm, Algorithm):
         raise TypeError('%r is not a matching algorithm object' %
             algorithm)
     
     self.current_location = None
     self.previous_location = None
     self._debug_enabled = debug
     self._running = False
     self._within_piece = False
     self._startup_condition = Condition()
     self._piece_control = Condition()
     
     self._playing_piece = False
     self._history = HistoryQueue()
     self.intervals = None
     self._incoming_notes = None
     
     self.interpreter = interpreter
     self.change_listener = change_listener
     self.progress_listener = progress_listener
     
     algorithm.assign_matcher(self)
     self._algorithm = algorithm
     
     self._thread = None
     self._stopping = False
开发者ID:enaeseth,项目名称:musicstand,代码行数:28,代码来源:matcher.py

示例11: __init__

 def __init__(self, number_threads):
   self.pool_lock = Lock()
   self.connection_available = Condition(self.pool_lock)
   self.request_available = Condition(self.pool_lock)
   self.connection_pool = []
   self.number_connections = 0
   self.max_connections = number_threads
开发者ID:Gerst20051,项目名称:Python,代码行数:7,代码来源:smtp_server.py

示例12: __init__

 def __init__(self):
     self.lock = Lock()
     self.toWrite = Condition(self.lock)
     self.toBackup = Condition(self.lock)
     self.isWriting = False
     self.isBackup = False
     self.emailNum = 1
开发者ID:ninefu,项目名称:Filesystem-with-SMTP-Mailsever,代码行数:7,代码来源:MailServer.py

示例13: __init__

 def __init__(self):
     # TODO
     self.carsCrossingNorth = 0
     self.carsCrossingSouth = 0
     self.cvLock = Lock()
     self.southQ = Condition(self.cvLock)
     self.northQ = Condition(self.cvLock)
开发者ID:johncurcio,项目名称:cs4410,代码行数:7,代码来源:q04-bridge-mon.py

示例14: __init__

class Future:
    def __init__(self, correlation_id, request_type=None):
        self.correlation_id = correlation_id
        self._result = None
        self._condition = Condition()
        self._request_type = request_type

    def done(self):
        return self._result is not None

    def result(self, timeout=None):
        with self._condition:
            if self._result is None:
                if not self._condition.wait(timeout):
                    message_type = validator_pb2.Message.MessageType.Name(
                        self._request_type) if self._request_type else None
                    raise FutureTimeoutError(
                        'Future timed out waiting for response to {}'.format(
                            message_type))
        return self._result

    def set_result(self, result):
        with self._condition:
            self._result = result
            self._condition.notify()
开发者ID:cianx,项目名称:sawtooth-core,代码行数:25,代码来源:future.py

示例15: Presponse

class Presponse(PktHandler):
    def __init__(self):
        self.ping_response_wait = Condition()
        '''
        To wait for a ping response, do the following:{
        start = time.time()
        with Presponse.ping_response_wait:
            Presponse.ping_response_wait.wait(5) # Standard timeout in PING latency is 5.
        end = time.time()
        }
        (end - start) is the ping latency.
        '''

    def handle(self, packet_map):
        with self.ping_response_wait:
            self.ping_response_wait.notify_all()
        return None

    def getproperties(self):
        return {
            'DisplayName':'Ping-Response Notifier',
            'CodeName':'PRESPONSE_NOTIFY',
            'PacketType':'PRESPONSE',
            'Version':0.01
        }
开发者ID:broad-well,项目名称:lightweight-iot-protocol,代码行数:25,代码来源:StandardModules.py


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