本文整理汇总了Python中threading.Barrier方法的典型用法代码示例。如果您正苦于以下问题:Python threading.Barrier方法的具体用法?Python threading.Barrier怎么用?Python threading.Barrier使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类threading
的用法示例。
在下文中一共展示了threading.Barrier方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_different_key_different_thread_wait
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Barrier [as 别名]
def test_different_key_different_thread_wait(self):
name1 = str(uuid.uuid4())
name2 = str(uuid.uuid4())
barrier = threading.Barrier(2)
thread = ExThread(target=thread_run, args=(name2, True), daemon=True)
thread.start()
thread.join()
lock1 = NamedReverseSemaphore(name1)
with lock1:
thread = ExThread(target=thread_run, args=(name2, True, barrier), daemon=True)
thread.start()
barrier.wait()
# FIXME: hack to make sure we acquired the lock in the other thread
time.sleep(0.2)
thread.join()
示例2: test_multiple_keys_different_thread
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Barrier [as 别名]
def test_multiple_keys_different_thread(self):
name1 = str(uuid.uuid4())
name2 = str(uuid.uuid4())
name3 = str(uuid.uuid4())
barrier = threading.Barrier(3)
threads = []
lock1 = NamedReverseSemaphore(name1)
with lock1:
threads.insert(0, ExThread(target=thread_run, args=(name2, True, barrier), daemon=True))
threads[0].start()
threads.insert(0, ExThread(target=thread_run, args=(name3, True, barrier), daemon=True))
threads[0].start()
barrier.wait()
# FIXME: hack to make sure we acquired the lock in the other thread
time.sleep(0.2)
for thread in threads:
thread.join()
示例3: test_multiple_keys_multiple_times_different_thread
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Barrier [as 别名]
def test_multiple_keys_multiple_times_different_thread(self):
name1 = str(uuid.uuid4())
name2 = str(uuid.uuid4())
name3 = str(uuid.uuid4())
barrier = threading.Barrier(5)
threads = []
lock1 = NamedReverseSemaphore(name1)
with lock1:
threads.insert(0, ExThread(target=thread_run, args=(name2, True, barrier), daemon=True))
threads[0].start()
threads.insert(0, ExThread(target=thread_run, args=(name2, True, barrier), daemon=True))
threads[0].start()
threads.insert(0, ExThread(target=thread_run, args=(name3, True, barrier), daemon=True))
threads[0].start()
threads.insert(0, ExThread(target=thread_run, args=(name3, True, barrier), daemon=True))
threads[0].start()
barrier.wait()
# FIXME: hack to make sure we acquired the lock in the other thread
time.sleep(0.2)
for thread in threads:
thread.join()
示例4: __init__
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Barrier [as 别名]
def __init__(self, rank, world_size, init_ttp=False):
self.world_size = world_size
self.rank = rank
self.reset_communication_stats()
self._name = f"rank{rank}"
with InProcessCommunicator.lock:
if InProcessCommunicator.mailbox is None:
InProcessCommunicator.mailbox = [
Queue() for _ in range(self.world_size)
]
# This prevents one thread from running ahead of the others and doing
# multiple puts that would show up in the get calls below
InProcessCommunicator.barrier = threading.Barrier(self.world_size)
# logging:
level = logging.getLogger().level
logging.getLogger().setLevel(logging.INFO)
logging.info("==================")
logging.info("InProcessCommunicator with rank %d" % self.rank)
logging.info("==================")
logging.info("World size = %d" % self.get_world_size())
logging.getLogger().setLevel(level)
示例5: test_allocator_thread_local
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Barrier [as 别名]
def test_allocator_thread_local(self):
def thread_body(self):
new_pool = memory.MemoryPool()
with cupy.cuda.using_allocator(new_pool.malloc):
assert memory.get_allocator() == new_pool.malloc
threading.Barrier(2)
arr = cupy.zeros(128, dtype=cupy.int64)
threading.Barrier(2)
self.assertEqual(arr.data.mem.size, new_pool.used_bytes())
threading.Barrier(2)
assert memory.get_allocator() == self.pool.malloc
with cupy.cuda.Device(0):
t = threading.Thread(target=thread_body, args=(self,))
t.daemon = True
t.start()
threading.Barrier(2)
assert memory.get_allocator() == self.pool.malloc
arr = cupy.ones(256, dtype=cupy.int64)
threading.Barrier(2)
self.assertEqual(arr.data.mem.size, self.pool.used_bytes())
threading.Barrier(2)
t.join()
示例6: wait
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Barrier [as 别名]
def wait(self, timeout=None):
self._cond.acquire()
try:
if self._flag.acquire(False):
self._flag.release()
else:
self._cond.wait(timeout)
if self._flag.acquire(False):
self._flag.release()
return True
return False
finally:
self._cond.release()
#
# Barrier
#
示例7: run_multiple_writers
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Barrier [as 别名]
def run_multiple_writers(self, writer, num_writers=32):
barrier = threading.Barrier(num_writers)
def writer_proxy(thread_index, results):
barrier.wait()
# Attempts to operate on a table while locked should raise a RuntimeError
try:
writer(thread_index, results)
results[thread_index] = 0
except RuntimeError:
results[thread_index] = 1
results = run_threads(writer_proxy, num_writers)
failures = sum(results)
successes = num_writers - failures
# Note: we would like to insist that #failures is > 0, but this is too
# stochastic to guarantee for test purposes.
self.assertGreaterEqual(failures, 0)
self.assertGreater(successes, 0)
示例8: test_safe_logging
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Barrier [as 别名]
def test_safe_logging():
barrier = Barrier(2)
counter = itertools.count()
sink = NonSafeSink(1)
logger.add(sink, format="{message}", catch=False)
def threaded():
barrier.wait()
logger.info("___{}___", next(counter))
threads = [Thread(target=threaded) for _ in range(2)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
logger.remove()
assert sink.written in ("___0___\n___1___\n", "___1___\n___0___\n")
示例9: run
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Barrier [as 别名]
def run(self):
print("Thread {} working on something".format(threading.current_thread()))
time.sleep(random.randint(1,10))
print("Thread {} is joining {} waiting on Barrier".format(threading.current_thread(), self.barrier.n_waiting))
self.barrier.wait()
print("Barrier has been lifted, continuing with work")
示例10: __init__
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Barrier [as 别名]
def __init__(self, *args: Any, **kwargs: Any):
super(SteppingMultiThreadedRunner, self).__init__(*args, **kwargs)
self.seen_prompt_events: Dict[str, Event] = {}
self.fetch_barrier: Optional[Barrier] = None
self.execute_barrier: Optional[Barrier] = None
self.application_threads: Dict[str, BarrierControlledApplicationThread] = {}
self.clock_thread = None
self.stop_event = Event()
示例11: start
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Barrier [as 别名]
def start(self) -> None:
super(SteppingMultiThreadedRunner, self).start()
parties = 1 + len(self.processes)
self.fetch_barrier = Barrier(parties)
self.execute_barrier = Barrier(parties)
# Create an event for each process.
for process_name in self.processes:
self.seen_prompt_events[process_name] = Event()
# Construct application threads.
for process_name, process in self.processes.items():
process_instance_id = process_name
thread = BarrierControlledApplicationThread(
process=process,
fetch_barrier=self.fetch_barrier,
execute_barrier=self.execute_barrier,
stop_event=self.stop_event,
)
self.application_threads[process_instance_id] = thread
# Start application threads.
for thread in self.application_threads.values():
thread.start()
# Start clock thread.
self.clock_thread = BarrierControllingClockThread(
normal_speed=self.normal_speed,
scale_factor=self.scale_factor,
tick_interval=self.tick_interval,
fetch_barrier=self.fetch_barrier,
execute_barrier=self.execute_barrier,
stop_event=self.stop_event,
is_verbose=self.is_verbose,
)
self.clock_thread.start()
示例12: roll_numpy_batches
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Barrier [as 别名]
def roll_numpy_batches(array, batch_size, shift_ratio):
"""Moves a proportion of batches from start to the end of the array.
This function moves a proportion of batches, specified by `shift_ratio`, from
the starts of the array to the end. The number of batches moved is rounded
down to the nearest integer. For example,
```
roll_numpy_batches([1, 2, 3, 4, 5, 6], 2, 0.34) == [3, 4, 5, 6, 1, 2]
```
Args:
array: A Numpy array whose first dimension is the batch dimension.
batch_size: The batch size.
shift_ratio: Proportion of batches to move from the start of the array to
the end of the array.
Returns:
A new Numpy array, with a proportion of the batches at the start of `array`
moved to the end.
"""
num_items = array.shape[0]
assert num_items % batch_size == 0
num_batches = num_items // batch_size
starting_batch = int(num_batches * shift_ratio)
starting_item = starting_batch * batch_size
return np.roll(array, -starting_item, axis=0)
# For Python 2.7 compatibility, we do not use threading.Barrier.
示例13: __init__
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Barrier [as 别名]
def __init__(self, sess, put_ops, batch_group_size, use_python32_barrier):
self.sess = sess
self.num_gets = 0
self.put_ops = put_ops
self.batch_group_size = batch_group_size
self.done_event = threading.Event()
if (use_python32_barrier and
sys.version_info[0] == 3 and sys.version_info[1] >= 2):
self.put_barrier = threading.Barrier(2)
else:
self.put_barrier = Barrier(2)
示例14: __init__
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Barrier [as 别名]
def __init__(self, sess, put_ops, batch_group_size):
self.sess = sess
self.num_gets = 0
self.put_ops = put_ops
self.batch_group_size = batch_group_size
self.done_event = threading.Event()
if (FLAGS.use_python32_barrier and
sys.version_info[0] == 3 and sys.version_info[1] >= 2):
self.put_barrier = threading.Barrier(2)
else:
self.put_barrier = Barrier(2)
示例15: test_lru_cache_threaded2
# 需要导入模块: import threading [as 别名]
# 或者: from threading import Barrier [as 别名]
def test_lru_cache_threaded2(self):
# Simultaneous call with the same arguments
n, m = 5, 7
start = threading.Barrier(n+1)
pause = threading.Barrier(n+1)
stop = threading.Barrier(n+1)
@self.module.lru_cache(maxsize=m*n)
def f(x):
pause.wait(10)
return 3 * x
self.assertEqual(f.cache_info(), (0, 0, m*n, 0))
def test():
for i in range(m):
start.wait(10)
self.assertEqual(f(i), 3 * i)
stop.wait(10)
threads = [threading.Thread(target=test) for k in range(n)]
with support.start_threads(threads):
for i in range(m):
start.wait(10)
stop.reset()
pause.wait(10)
start.reset()
stop.wait(10)
pause.reset()
self.assertEqual(f.cache_info(), (0, (i+1)*n, m*n, i+1))