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

Python multiprocessing.Condition类代码示例

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

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



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

示例1: __init__

    def __init__(self):

        self.pcb = None

        self.__mutex = RLock()
        self.__pcb_not_set = Condition(self.__mutex)
        self.__mem_not_allocated = Condition(self.__mutex)
        self.__round_robin_policy_on = False
开发者ID:danwyryunq,项目名称:TPSOUNQ,代码行数:8,代码来源:Cpu.py


示例2: __init__

 def __init__(self, a_device, a_kernel, std_in=StandardInput(), std_out=StandardOutput()):
     Thread.__init__(self)
     self.set_device(a_device)
     self.set_kernel(a_kernel)
     self.set_input(std_in)
     self.set_output(std_out)
     self.set_mutex(RLock())
     self.set_queue(SoQueue())
     self.device_is_in_use = Condition(self.get_mutex())
     self.the_queue_is_empty = Condition(self.get_mutex())
开发者ID:danwyryunq,项目名称:TPSOUNQ,代码行数:10,代码来源:IODeviceManager.py


示例3: __init__

 def __init__(self, * args):
     TServer.__init__(self, *args)
     self.numWorkers = 10
     self.workers = []
     self.isRunning = Value('b', False)
     self.stopCondition = Condition()
     self.postForkCallback = None
开发者ID:csabahruska,项目名称:lambdacube.addon,代码行数:7,代码来源:TProcessPoolServer.py


示例4: Barrier

class Barrier(object):
    def __init__(self, num_threads):
        self.num_threads = num_threads
        self.threads_left = Value('i', num_threads, lock=True)
        self.mutex = Lock()
        self.waitcond = Condition(self.mutex)

    def wait(self):
        self.mutex.acquire()
        self.threads_left.value -= 1
        if self.threads_left.value == 0:
            self.threads_left.value = self.num_threads
            self.waitcond.notify_all()
            self.mutex.release()
        else:
            self.waitcond.wait()
            self.mutex.release()
开发者ID:Jailander,项目名称:mongodb_store,代码行数:17,代码来源:mongodb_log.py


示例5: __init__

    def __init__(self):
        """Initialises the RWLock."""
        self._condition = Condition()
        self._readers = Value(c_uint64, 0, lock=False)
        self._writers_waiting = Value(c_uint64, 0, lock=False)

        self.for_reading = self.ReadLock(self)
        self.for_writing = self.WriteLock(self)
开发者ID:PaperclipBadger,项目名称:hadamardequivalence,代码行数:8,代码来源:concurrencyutils.py


示例6: __call__

    def __call__(self, cv_iterator, evaluator, fold_callback=None,
                 n_jobs=None):
        """
        """
        condvar = Condition()
        results = []

        def _signal_cb(result):
            condvar.acquire()
            results.append(result)
            condvar.notify()
            condvar.release()
        folds = list(cv_iterator)

        pool, deferreds = self.async(folds, evaluator,
                                     fold_callback=_signal_cb, n_jobs=n_jobs)
        pool.close()
        while len(results) < len(folds):
            condvar.acquire()
            condvar.wait()
            fold_estimator, result = results[-1]
            fold_callback(fold_estimator, result)
            condvar.release()
        pool.join()
        return results
开发者ID:ihaque,项目名称:grizzly,代码行数:25,代码来源:multiprocess.py


示例7: __init__

 def __init__(self, maxsize):
     self.queue = Queue(maxsize=maxsize)
     self.lock = Lock()
     self.getlock = Lock()
     self.putcounter = Value('i', -1)
     self.getcounter = Value('i', 0)
     self.cond = Condition(self.lock)
     self.manager = Manager()
     self.getlist = self.manager.list()
开发者ID:SinaHonari,项目名称:RCN,代码行数:9,代码来源:queue.py


示例8: activate_as_parent

 def activate_as_parent(self, debug=False):
     assert not self.child_mode
     self.debug_mode = debug
     self.jobs = []
     self.output_lock = Lock()
     self.parent_mode = True
     self.output_queue = Queue()
     self.status_line_cleared = Condition()
     self.thread = Thread(target=self._print_thread)
     self.thread.daemon = True
     self.thread.start()
开发者ID:sq3,项目名称:bundlewrap,代码行数:11,代码来源:ui.py


示例9: __init__

 def __init__(self, engine, max_working = 1):
     self.condition     = Condition(RLock())
     self.engine        = engine
     self.max_working   = max_working
     self.running       = False
     self.paused        = False
     self.metadata      = sa.MetaData(self.engine)
     self._table_prefix = 'exscript_pipeline_'
     self._table_map    = {}
     self.__update_table_names()
     self.clear()
开发者ID:0x24bin,项目名称:exscript,代码行数:11,代码来源:DBPipeline.py


示例10: __init__

 def __init__(self):
     super(CountBucket, self).__init__()
     self.matches = set([])
     self.runtime_stats_query_fun = None
     self.outstanding_switches = []
     self.packet_count = 0
     self.byte_count = 0
     self.packet_count_persistent = 0
     self.byte_count_persistent = 0
     self.in_update_cv = Condition()
     self.in_update = False
开发者ID:Milstein,项目名称:pyretic,代码行数:11,代码来源:language.py


示例11: OrderedQueue

class OrderedQueue(object):
    def __init__(self, maxsize):
        self.queue = Queue(maxsize=maxsize)
        self.lock = Lock()
        self.getlock = Lock()
        self.putcounter = Value('i', -1)
        self.getcounter = Value('i', 0)
        self.cond = Condition(self.lock)
        self.manager = Manager()
        self.getlist = self.manager.list()

    def put(self, index, elem):
        with self.lock:
            while index != self.putcounter.value + 1:
                self.cond.wait()
            self.queue.put((index, elem))
            #sys.stderr.write("right after adding data with SEED %i. Queue size is now %i\n" %(index, self.queue.qsize()))
            self.putcounter.value += 1
            self.cond.notify_all()
        
    def get(self):
        with self.getlock:
            for i, element in enumerate(self.getlist):
                index, elem = element 
                if index == self.getcounter.value:
                    self.getcounter.value += 1
                    del self.getlist[i]
                    return (index, elem)
            while True:
                index, elem = self.queue.get()
                if index == self.getcounter.value:
                    self.getcounter.value += 1
                    return (index, elem)
                else:
                    self.getlist.append((index, elem))

    def close(self):
        return self.queue.close()

    def qsize(self):
        return self.queue.qsize()
开发者ID:SinaHonari,项目名称:RCN,代码行数:41,代码来源:queue.py


示例12: test_watch_directory

def test_watch_directory():

    def _cleanup(path):
        for f in listdir(path):
            p = join(path, f)
            if isdir(p):
                rmtree(p)
            elif f != '.nothing':
                unlink(p)

    sample_template = ''
    sample_directory = dirname(realpath(__file__)) + '/sample/'
    watch_directory = sample_directory + 'watch/'
    render_directory = sample_directory + 'render/'
    template_directory = sample_directory + 'templates/'
    with open(template_directory + 'haml.tmpl', 'r') as f:
        sample_template = f.read()

    condition = Condition()
    p = Process(target=reloader.watch_directory,
                args=(watch_directory, render_directory, condition))
    condition.acquire()
    p.start()
    condition.wait()

    try:
        with open(watch_directory + 'test.haml', 'w') as f:
            f.write(sample_template)

        subdir = watch_directory + 'test_subdir/'
        try:
            mkdir(subdir)
        except OSError:
            if not isdir(subdir):
                raise
        with open(subdir + 'test_two.haml', 'w') as f:
            f.write(sample_template)

        sleep(1)

        assert_true(exists(render_directory + 'test.html'))
        assert_true(exists(render_directory + 'test_subdir/test_two.html'))
    except:
        raise
    finally:
        condition.release()
        p.terminate()
        p.join()

        sleep(1)

        _cleanup(watch_directory)
        _cleanup(render_directory)
开发者ID:petermelias,项目名称:hamlreloader,代码行数:53,代码来源:test_reloader.py


示例13: __init__

 def __init__(self, max_working = 1):
     self.condition   = Condition(RLock())
     self.max_working = max_working
     self.running     = True
     self.paused      = False
     self.queue       = None
     self.force       = None
     self.sleeping    = None
     self.working     = None
     self.item2id     = None
     self.id2item     = None # for performance reasons
     self.name2id     = None
     self.id2name     = None
     self.clear()
开发者ID:0x24bin,项目名称:exscript,代码行数:14,代码来源:Pipeline.py


示例14: __init__

    def __init__(self, world_class, opt, agents):
        self.inner_world = world_class(opt, agents)

        self.queued_items = Semaphore(0)  # counts num exs to be processed
        self.epochDone = Condition()  # notifies when exs are finished
        self.terminate = Value('b', False)  # tells threads when to shut down
        self.cnt = Value('i', 0)  # number of exs that remain to be processed

        self.threads = []
        for i in range(opt['numthreads']):
            self.threads.append(HogwildProcess(i, world_class, opt,
                                               agents, self.queued_items,
                                               self.epochDone, self.terminate,
                                               self.cnt))
        for t in self.threads:
            t.start()
开发者ID:jojonki,项目名称:ParlAI,代码行数:16,代码来源:worlds.py


示例15: SynchronizingBus

class SynchronizingBus(Bus):
    def __init__(self, sync_delay=1):
        Bus.__init__(self)
        self.sync_delay = sync_delay
        self.condition = Condition()

    def start(self):
        import time
        time.sleep(self.sync_delay)
        self.log("Releasing children")
        self.condition.acquire()
        self.condition.notify_all()
        self.condition.release()
        Bus.start(self)
开发者ID:Lawouach,项目名称:conductor,代码行数:14,代码来源:bus.py


示例16: start

    def start(self):
        if self._started:
            return

        self.manager = manager = Manager()
        self.shared_uuid_fn_dict = manager.dict()
        self.shared_uuid_map_dict = manager.dict()
        self.shared_master_blocks = manager.dict()
        self.download_cond = Condition()

        self._started = True
        self.ctx = zmq.Context()
        self.host = socket.gethostname()
        if GUIDE_ADDR not in env.environ:
            start_guide_manager()

        self.guide_addr = env.get(GUIDE_ADDR)
        self.random_inst = random.SystemRandom()
        self.server_addr, self.server_thread = self.start_server()
        self.uuid_state_dict = {}
        self.uuid_map_dict = {}
        self.master_broadcast_blocks = {}
        env.register(DOWNLOAD_ADDR, self.server_addr)
开发者ID:douban,项目名称:dpark,代码行数:23,代码来源:broadcast.py


示例17: DownloadManager

class DownloadManager(object):
    def __init__(self):
        self._started = False
        self.server_thread = None
        self.download_threads = {}
        self.uuid_state_dict = None
        self.uuid_map_dict = None
        self.guide_addr = None
        self.server_addr = None
        self.host = None
        self.ctx = None
        self.random_inst = None
        self.master_broadcast_blocks = {}

    def start(self):
        if self._started:
            return

        self.manager = manager = Manager()
        self.shared_uuid_fn_dict = manager.dict()
        self.shared_uuid_map_dict = manager.dict()
        self.shared_master_blocks = manager.dict()
        self.download_cond = Condition()

        self._started = True
        self.ctx = zmq.Context()
        self.host = socket.gethostname()
        if GUIDE_ADDR not in env.environ:
            start_guide_manager()

        self.guide_addr = env.get(GUIDE_ADDR)
        self.random_inst = random.SystemRandom()
        self.server_addr, self.server_thread = self.start_server()
        self.uuid_state_dict = {}
        self.uuid_map_dict = {}
        self.master_broadcast_blocks = {}
        env.register(DOWNLOAD_ADDR, self.server_addr)

    def start_server(self):
        sock = self.ctx.socket(zmq.REP)
        sock.setsockopt(zmq.LINGER, 0)
        port = sock.bind_to_random_port("tcp://0.0.0.0")
        server_addr = 'tcp://%s:%d' % (self.host, port)
        guide_sock = self.ctx.socket(zmq.REQ)
        guide_sock.setsockopt(zmq.LINGER, 0)
        guide_sock.connect(self.guide_addr)

        def run():
            logger.debug("server started at %s", server_addr)

            while self._started:
                if not sock.poll(1000, zmq.POLLIN):
                    continue
                type_, msg = sock.recv_pyobj()
                logger.debug('server recv: %s %s', type_, msg)
                if type_ == SERVER_STOP:
                    sock.send_pyobj(None)
                    break
                elif type_ == SERVER_FETCH:
                    uuid, indices, client_addr = msg
                    if uuid in self.master_broadcast_blocks:
                        block_num = len(self.master_broadcast_blocks[uuid])
                        bls = []
                        for index in indices:
                            if index >= block_num:
                                logger.warning('input index too big %s for '
                                               'len of blocks  %d from host %s',
                                               str(indices), block_num, client_addr)
                                sock.send_pyobj((SERVER_FETCH_FAIL, None))
                            else:
                                bls.append(self.master_broadcast_blocks[uuid][index])
                        sock.send_pyobj((SERVER_FETCH_OK, (indices, bls)))
                    elif uuid in self.uuid_state_dict:
                        fd = os.open(self.uuid_state_dict[uuid][0], os.O_RDONLY)
                        mmfp = mmap.mmap(fd, 0, access=ACCESS_READ)
                        os.close(fd)
                        bitmap = self.uuid_map_dict[uuid]
                        block_num = len(bitmap)
                        bls = []
                        for index in indices:
                            if index >= block_num:
                                logger.warning('input index too big %s for '
                                               'len of blocks  %d from host %s',
                                               str(indices), block_num, client_addr)
                                sock.send_pyobj((SERVER_FETCH_FAIL, None))
                            else:
                                mmfp.seek(bitmap[index][0])
                                block = mmfp.read(bitmap[index][1])
                                bls.append(block)
                        mmfp.close()
                        sock.send_pyobj((SERVER_FETCH_OK, (indices, bls)))
                    else:
                        logger.warning('server fetch failed for uuid %s '
                                       'not exists in server %s from host %s',
                                       uuid, socket.gethostname(), client_addr)
                        sock.send_pyobj((SERVER_FETCH_FAIL, None))
                elif type_ == DATA_GET:
                    uuid, compressed_size = msg
                    if uuid not in self.uuid_state_dict or not self.uuid_state_dict[uuid][1]:
                        if uuid not in self.download_threads:
#.........这里部分代码省略.........
开发者ID:douban,项目名称:dpark,代码行数:101,代码来源:broadcast.py


示例18: __init__

 def __init__(self, num_threads):
     self.num_threads = num_threads
     self.threads_left = Value('i', num_threads, lock=True)
     self.mutex = Lock()
     self.waitcond = Condition(self.mutex)
开发者ID:Jailander,项目名称:mongodb_store,代码行数:5,代码来源:mongodb_log.py


示例19: WaitableQueue

class WaitableQueue(Queue):
    """Queue that uses a semaphore to reliably count items in it"""
    class Vacuum(ThreadLoop):
        def __init__(self, q, l):
            def callback():
                q.wait_notempty(0.1)

                while True:
                    try:
                        val = q.get(False)
                        l.append(val)

                    except Empty:
                        break

            ThreadLoop.__init__(self, callback)

    def __init__(self, maxsize=0):
        self.cond_empty = Condition()
        self.cond_notempty = Condition()
        self._put_counter = Value('i', 0)

        Queue.__init__(self, maxsize)

    def put(self, obj, block=True, timeout=None):
        Queue.put(self, obj, block, timeout)
        self._put_counter.value += 1

        if self.qsize() != 0:
            self.cond_notempty.acquire()
            try:
                self.cond_notempty.notify_all()
            finally:
                self.cond_notempty.release()

    @property
    def put_counter(self):
        return self._put_counter.value

    def get(self, block=True, timeout=None):
        ret = Queue.get(self, block, timeout)
        if self.qsize() == 0:
            self.cond_empty.acquire()
            try:
                self.cond_empty.notify_all()
            finally:
                self.cond_empty.release()

        return ret

    def wait_empty(self, timeout=None):
        """Wait for all items to be got"""
        self.cond_empty.acquire()
        try:
            if self.qsize():
                self.cond_empty.wait(timeout)
        finally:
            self.cond_empty.release()

    def wait_notempty(self, timeout=None):
        """Wait for all items to be got"""
        self.cond_notempty.acquire()
        try:
            if self.qsize() == 0:
                self.cond_notempty.wait(timeout)
        finally:
            self.cond_notempty.release()
开发者ID:OnGle,项目名称:turnkey-pylib,代码行数:67,代码来源:multiprocessing_utils.py


示例20: IODeviceManager

class IODeviceManager(Thread):

    def __init__(self, a_device, a_kernel, std_in=StandardInput(), std_out=StandardOutput()):
        Thread.__init__(self)
        self.set_device(a_device)
        self.set_kernel(a_kernel)
        self.set_input(std_in)
        self.set_output(std_out)
        self.set_mutex(RLock())
        self.set_queue(SoQueue())
        self.device_is_in_use = Condition(self.get_mutex())
        self.the_queue_is_empty = Condition(self.get_mutex())

    def get_kernel(self):
        return self.kernel

    def set_kernel(self, a_kernel):
        self.kernel = a_kernel

    def set_input(self, a_input):
        self.std_in = a_input

    def get_input(self):
        return self.std_in

    def set_output(self, a_output):
        self.std_out = a_output

    def get_output(self):
        return self.std_out

    def get_mutex(self):
        return self.mutex
        
    def set_mutex(self, a_mutex):   
        self.mutex = a_mutex
        
    def get_queue(self):
        return self.queue
        
    def set_queue(self, a_queue):
        self.queue = a_queue
    
    def set_device(self, a_device):
        self.device = a_device
        self.get_device().set_device_manager(self)

    def get_device(self):
        return self.device
        
    def the_device_is_busy(self):
        with self.get_mutex():
            return not self.get_device().is_not_busy()
    
    def send_to_device(self):
        with self.device_is_in_use:
            while self.the_device_is_busy():
                self.device_is_in_use.wait()
            with self.get_mutex():
                self.get_device().set_pcb(self.get())
                self.get_device().process_pcb()

    def notify_that_the_device_is_not_in_use(self):
        with self.device_is_in_use:
            self.device_is_in_use.notify()
    
    def put(self, a_pcb):
        with self.the_queue_is_empty:
            with self.get_mutex():
                self.get_queue().add_pcb(a_pcb)
                self.the_queue_is_empty.notify()
        
    def get(self):
        with self.get_mutex():
            return self.get_queue().get_first()

    def queue_is_empty(self):
        return self.get_queue().is_empty()

    def send_io_end_interruption(self, a_pcb):
        self.get_kernel().get_irq_manager().handle(Irq(IO_END_INTERRUPT,  a_pcb))
            
    def run(self):
        while True:
            with self.the_queue_is_empty:
                while self.queue_is_empty():
                    self.the_queue_is_empty.wait()
                self.send_to_device()
开发者ID:danwyryunq,项目名称:TPSOUNQ,代码行数:88,代码来源:IODeviceManager.py



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Python multiprocessing.Event类代码示例发布时间:2022-05-27
下一篇:
Python multiprocessing.Array类代码示例发布时间: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