Python multiprocessing.Barrier() Examples
The following are 27
code examples of multiprocessing.Barrier().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
multiprocessing
, or try the search function
.
Example #1
Source File: test_multiprocessing.py From loguru with MIT License | 7 votes |
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 == ""
Example #2
Source File: test_multiprocessing.py From loguru with MIT License | 7 votes |
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"
Example #3
Source File: test_multiprocessing.py From loguru with MIT License | 7 votes |
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"
Example #4
Source File: test_ringbuffer.py From ringbuffer with Apache License 2.0 | 6 votes |
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()
Example #5
Source File: test_multiprocessing.py From loguru with MIT License | 6 votes |
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"
Example #6
Source File: async_rl.py From rlpyt with MIT License | 6 votes |
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), )
Example #7
Source File: test_ringbuffer.py From ringbuffer with Apache License 2.0 | 6 votes |
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()
Example #8
Source File: test_zip_container.py From pfio with MIT License | 6 votes |
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)
Example #9
Source File: _test_multiprocessing.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
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)
Example #10
Source File: test_ringbuffer.py From ringbuffer with Apache License 2.0 | 5 votes |
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()
Example #11
Source File: test_ringbuffer.py From ringbuffer with Apache License 2.0 | 5 votes |
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()
Example #12
Source File: _test_multiprocessing.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
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)
Example #13
Source File: _test_multiprocessing.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
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)
Example #14
Source File: _test_multiprocessing.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
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)
Example #15
Source File: _test_multiprocessing.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def setUp(self): self.barrier = self.Barrier(self.N, timeout=self.defaultTimeout)
Example #16
Source File: sync_rl.py From rlpyt with MIT License | 5 votes |
def build_par_objs(self, world_size): barrier = mp.Barrier(world_size) traj_infos_queue = mp.Queue() par = AttrDict( barrier=barrier, traj_infos_queue=traj_infos_queue, ) return par
Example #17
Source File: base.py From rlpyt with MIT License | 5 votes |
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))
Example #18
Source File: _test_multiprocessing.py From ironpython3 with Apache License 2.0 | 5 votes |
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)
Example #19
Source File: _test_multiprocessing.py From ironpython3 with Apache License 2.0 | 5 votes |
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)
Example #20
Source File: _test_multiprocessing.py From ironpython3 with Apache License 2.0 | 5 votes |
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)
Example #21
Source File: _test_multiprocessing.py From ironpython3 with Apache License 2.0 | 5 votes |
def setUp(self): self.barrier = self.Barrier(self.N, timeout=self.defaultTimeout)
Example #22
Source File: _test_multiprocessing.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
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)
Example #23
Source File: _test_multiprocessing.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
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)
Example #24
Source File: _test_multiprocessing.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def setUp(self): self.barrier = self.Barrier(self.N, timeout=self.defaultTimeout)
Example #25
Source File: _test_multiprocessing.py From Fluid-Designer with GNU General Public License v3.0 | 4 votes |
def test_event(self): event = self.Event() wait = TimingWrapper(event.wait) # Removed temporarily, due to API shear, this does not # work with threading._Event objects. is_set == isSet self.assertEqual(event.is_set(), False) # Removed, threading.Event.wait() will return the value of the __flag # instead of None. API Shear with the semaphore backed mp.Event self.assertEqual(wait(0.0), False) self.assertTimingAlmostEqual(wait.elapsed, 0.0) self.assertEqual(wait(TIMEOUT1), False) self.assertTimingAlmostEqual(wait.elapsed, TIMEOUT1) event.set() # See note above on the API differences self.assertEqual(event.is_set(), True) self.assertEqual(wait(), True) self.assertTimingAlmostEqual(wait.elapsed, 0.0) self.assertEqual(wait(TIMEOUT1), True) self.assertTimingAlmostEqual(wait.elapsed, 0.0) # self.assertEqual(event.is_set(), True) event.clear() #self.assertEqual(event.is_set(), False) p = self.Process(target=self._test_event, args=(event,)) p.daemon = True p.start() self.assertEqual(wait(), True) # # Tests for Barrier - adapted from tests in test/lock_tests.py # # Many of the tests for threading.Barrier use a list as an atomic # counter: a value is appended to increment the counter, and the # length of the list gives the value. We use the class DummyList # for the same purpose.
Example #26
Source File: _test_multiprocessing.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 4 votes |
def test_event(self): event = self.Event() wait = TimingWrapper(event.wait) # Removed temporarily, due to API shear, this does not # work with threading._Event objects. is_set == isSet self.assertEqual(event.is_set(), False) # Removed, threading.Event.wait() will return the value of the __flag # instead of None. API Shear with the semaphore backed mp.Event self.assertEqual(wait(0.0), False) self.assertTimingAlmostEqual(wait.elapsed, 0.0) self.assertEqual(wait(TIMEOUT1), False) self.assertTimingAlmostEqual(wait.elapsed, TIMEOUT1) event.set() # See note above on the API differences self.assertEqual(event.is_set(), True) self.assertEqual(wait(), True) self.assertTimingAlmostEqual(wait.elapsed, 0.0) self.assertEqual(wait(TIMEOUT1), True) self.assertTimingAlmostEqual(wait.elapsed, 0.0) # self.assertEqual(event.is_set(), True) event.clear() #self.assertEqual(event.is_set(), False) p = self.Process(target=self._test_event, args=(event,)) p.daemon = True p.start() self.assertEqual(wait(), True) # # Tests for Barrier - adapted from tests in test/lock_tests.py # # Many of the tests for threading.Barrier use a list as an atomic # counter: a value is appended to increment the counter, and the # length of the list gives the value. We use the class DummyList # for the same purpose.
Example #27
Source File: _test_multiprocessing.py From ironpython3 with Apache License 2.0 | 4 votes |
def test_event(self): event = self.Event() wait = TimingWrapper(event.wait) # Removed temporarily, due to API shear, this does not # work with threading._Event objects. is_set == isSet self.assertEqual(event.is_set(), False) # Removed, threading.Event.wait() will return the value of the __flag # instead of None. API Shear with the semaphore backed mp.Event self.assertEqual(wait(0.0), False) self.assertTimingAlmostEqual(wait.elapsed, 0.0) self.assertEqual(wait(TIMEOUT1), False) self.assertTimingAlmostEqual(wait.elapsed, TIMEOUT1) event.set() # See note above on the API differences self.assertEqual(event.is_set(), True) self.assertEqual(wait(), True) self.assertTimingAlmostEqual(wait.elapsed, 0.0) self.assertEqual(wait(TIMEOUT1), True) self.assertTimingAlmostEqual(wait.elapsed, 0.0) # self.assertEqual(event.is_set(), True) event.clear() #self.assertEqual(event.is_set(), False) p = self.Process(target=self._test_event, args=(event,)) p.daemon = True p.start() self.assertEqual(wait(), True) # # Tests for Barrier - adapted from tests in test/lock_tests.py # # Many of the tests for threading.Barrier use a list as an atomic # counter: a value is appended to increment the counter, and the # length of the list gives the value. We use the class DummyList # for the same purpose.