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


Python multiprocessing.Barrier方法代码示例

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


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

示例1: test_reader_blocks_writer

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import Barrier [as 别名]
def test_reader_blocks_writer(self):
        with self.lock.for_read():
            before_write = multiprocessing.Barrier(2)
            during_write = multiprocessing.Barrier(2)
            after_write = multiprocessing.Barrier(2)
            after_unlock = multiprocessing.Barrier(2)

            def test():
                self.assert_readers(1)

                before_write.wait()

                with self.lock.for_write():
                    self.assert_writer()
                    return 'written'

            writer = self.async(test)

            # Wait until we can confirm that all writers are locked out.
            before_write.wait()
            self.assert_readers(1)

        self.assertEqual('written', self.get_result(writer))
        self.assert_unlocked() 
开发者ID:bslatkin,项目名称:ringbuffer,代码行数:26,代码来源:test_ringbuffer.py

示例2: test_multiple_writers_block_each_other

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import Barrier [as 别名]
def test_multiple_writers_block_each_other(self):
        with self.lock.for_write():
            before_write = multiprocessing.Barrier(2)

            def test():
                before_write.wait()

                with self.lock.for_write():
                    self.assert_writer()
                    return 'written'

            writer = self.async(test)

            before_write.wait()
            self.assert_writer()

        self.assertEqual('written', self.get_result(writer))
        self.assert_unlocked() 
开发者ID:bslatkin,项目名称:ringbuffer,代码行数:20,代码来源:test_ringbuffer.py

示例3: test_read_multi_processes

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import Barrier [as 别名]
def test_read_multi_processes(self):
        barrier = multiprocessing.Barrier(2)
        with self.fs_handler.open_as_container(
                os.path.abspath(self.zip_file_path)) as handler:
            with handler.open(self.testfile_name) as f:
                f.read()

            def func():
                # accessing the shared container
                with handler.open(self.testfile_name) as f:
                    barrier.wait()
                    f.read()

            p1 = multiprocessing.Process(target=func)
            p2 = multiprocessing.Process(target=func)
            p1.start()
            p2.start()

            p1.join(timeout=1)
            p2.join(timeout=1)

            self.assertEqual(p1.exitcode, 0)
            self.assertEqual(p2.exitcode, 0) 
开发者ID:pfnet,项目名称:pfio,代码行数:25,代码来源:test_zip_container.py

示例4: build_ctrl

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import Barrier [as 别名]
def build_ctrl(self, world_size):
        """
        Builds several parallel communication mechanisms for controlling the
        workflow across processes.
        """
        opt_throttle = (mp.Barrier(world_size) if world_size > 1 else
            None)
        return AttrDict(
            quit=mp.Value('b', lock=True),
            quit_opt=mp.RawValue('b'),
            sample_ready=[mp.Semaphore(0) for _ in range(2)],  # Double buffer.
            sample_copied=[mp.Semaphore(1) for _ in range(2)],
            sampler_itr=mp.Value('l', lock=True),
            opt_throttle=opt_throttle,
            eval_time=mp.Value('d', lock=True),
        ) 
开发者ID:astooke,项目名称:rlpyt,代码行数:18,代码来源:async_rl.py

示例5: test_remove_in_main_process_spawn

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import Barrier [as 别名]
def test_remove_in_main_process_spawn(monkeypatch):
    # Actually, this test may fail if sleep time in main process is too small (and no barrier used)
    # In such situation, it seems the child process has not enough time to initialize itself
    # It may fail with an "EOFError" during unpickling of the (garbage collected / closed) Queue
    ctx = multiprocessing.get_context("spawn")
    monkeypatch.setattr(loguru._handler, "multiprocessing", ctx)

    writer = Writer()
    barrier = ctx.Barrier(2)

    logger.add(writer, format="{message}", enqueue=True, catch=False)

    process = ctx.Process(target=subworker_barrier, args=(logger, barrier))
    process.start()
    barrier.wait()
    logger.info("Main")
    logger.remove()
    process.join()

    assert process.exitcode == 0

    assert writer.read() == "Child\nMain\n" 
开发者ID:Delgan,项目名称:loguru,代码行数:24,代码来源:test_multiprocessing.py

示例6: test_remove_in_main_process_fork

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import Barrier [as 别名]
def test_remove_in_main_process_fork():
    writer = Writer()
    barrier = multiprocessing.Barrier(2)

    logger.add(writer, format="{message}", enqueue=True, catch=False)

    process = multiprocessing.Process(target=subworker_barrier, args=(logger, barrier))
    process.start()
    barrier.wait()
    logger.info("Main")
    logger.remove()
    process.join()

    assert process.exitcode == 0

    assert writer.read() == "Child\nMain\n" 
开发者ID:Delgan,项目名称:loguru,代码行数:18,代码来源:test_multiprocessing.py

示例7: test_remove_in_main_process_inheritance

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import Barrier [as 别名]
def test_remove_in_main_process_inheritance():
    writer = Writer()
    barrier = multiprocessing.Barrier(2)

    logger.add(writer, format="{message}", enqueue=True, catch=False)

    process = multiprocessing.Process(target=subworker_barrier_inheritance, args=(barrier,))
    process.start()
    barrier.wait()
    logger.info("Main")
    logger.remove()
    process.join()

    assert process.exitcode == 0

    assert writer.read() == "Child\nMain\n" 
开发者ID:Delgan,项目名称:loguru,代码行数:18,代码来源:test_multiprocessing.py

示例8: test_complete_from_multiple_child_processes

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import Barrier [as 别名]
def test_complete_from_multiple_child_processes(capsys):
    logger.add(lambda _: None, enqueue=True, catch=False)
    num = 100

    barrier = multiprocessing.Barrier(num)

    def worker(barrier):
        barrier.wait()
        logger.complete()

    processes = []

    for _ in range(num):
        process = multiprocessing.Process(target=worker, args=(barrier,))
        process.start()
        processes.append(process)

    for process in processes:
        process.join(5)
        assert process.exitcode == 0

    out, err = capsys.readouterr()
    assert out == err == "" 
开发者ID:Delgan,项目名称:loguru,代码行数:25,代码来源:test_multiprocessing.py

示例9: test_writer_blocks_multiple_readers

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import Barrier [as 别名]
def test_writer_blocks_multiple_readers(self):
        with self.lock.for_write():
            before_read = multiprocessing.Barrier(3)
            during_read = multiprocessing.Barrier(2)
            after_read = multiprocessing.Barrier(2)

            def test():
                self.assert_writer()

                before_read.wait()

                with self.lock.for_read():
                    during_read.wait()
                    value = self.reader_count()
                    after_read.wait()
                    return value

            r1 = self.async(test)
            r2 = self.async(test)

            # Wait until we can confirm that all readers are locked out
            before_read.wait()
            self.assert_writer()

        self.assertEqual(2, self.get_result(r1))
        self.assertEqual(2, self.get_result(r2))
        self.assert_unlocked() 
开发者ID:bslatkin,项目名称:ringbuffer,代码行数:29,代码来源:test_ringbuffer.py

示例10: test_multiple_readers_block_writer

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import Barrier [as 别名]
def test_multiple_readers_block_writer(self):
        with self.lock.for_read():
            before_read = multiprocessing.Barrier(3)
            after_read = multiprocessing.Barrier(2)

            def test_reader():
                self.assert_readers(1)

                with self.lock.for_read():
                    before_read.wait()
                    value = self.reader_count()
                    after_read.wait()
                    return value

            def test_writer():
                before_read.wait()

                with self.lock.for_write():
                    self.assert_writer()
                    return 'written'

            reader = self.async(test_reader)
            writer = self.async(test_writer)

            # Wait for the write to be blocked by multiple readers.
            before_read.wait()
            self.assert_readers(2)
            after_read.wait()

        self.assertEqual(2, self.get_result(reader))
        self.assertEqual('written', self.get_result(writer))
        self.assert_unlocked() 
开发者ID:bslatkin,项目名称:ringbuffer,代码行数:34,代码来源:test_ringbuffer.py

示例11: setUp

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import Barrier [as 别名]
def setUp(self):
        self.barrier = self.Barrier(self.N, timeout=self.defaultTimeout) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:4,代码来源:_test_multiprocessing.py

示例12: test_action

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import Barrier [as 别名]
def test_action(self):
        """
        Test the 'action' callback
        """
        results = self.DummyList()
        barrier = self.Barrier(self.N, action=AppendTrue(results))
        self.run_threads(self._test_action_f, (barrier, results))
        self.assertEqual(len(results), 1) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:10,代码来源:_test_multiprocessing.py

示例13: test_abort_and_reset

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import Barrier [as 别名]
def test_abort_and_reset(self):
        """
        Test that a barrier can be reset after being broken.
        """
        results1 = self.DummyList()
        results2 = self.DummyList()
        results3 = self.DummyList()
        barrier2 = self.Barrier(self.N)

        self.run_threads(self._test_abort_and_reset_f,
                         (self.barrier, barrier2, results1, results2, results3))
        self.assertEqual(len(results1), 0)
        self.assertEqual(len(results2), self.N-1)
        self.assertEqual(len(results3), self.N) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:16,代码来源:_test_multiprocessing.py

示例14: test_default_timeout

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import Barrier [as 别名]
def test_default_timeout(self):
        """
        Test the barrier's default timeout
        """
        barrier = self.Barrier(self.N, timeout=0.5)
        results = self.DummyList()
        self.run_threads(self._test_default_timeout_f, (barrier, results))
        self.assertEqual(len(results), barrier.parties) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:10,代码来源:_test_multiprocessing.py

示例15: _build_parallel_ctrl

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import Barrier [as 别名]
def _build_parallel_ctrl(self, n_worker):
        self.ctrl = AttrDict(
            quit=mp.RawValue(ctypes.c_bool, False),
            barrier_in=mp.Barrier(n_worker + 1),
            barrier_out=mp.Barrier(n_worker + 1),
            do_eval=mp.RawValue(ctypes.c_bool, False),
            itr=mp.RawValue(ctypes.c_long, 0),
        )
        self.traj_infos_queue = mp.Queue()
        self.eval_traj_infos_queue = mp.Queue()
        self.sync = AttrDict(stop_eval=mp.RawValue(ctypes.c_bool, False)) 
开发者ID:astooke,项目名称:rlpyt,代码行数:13,代码来源:base.py


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