Python threading.Barrier() Examples

The following are code examples for showing how to use threading.Barrier(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the ones you don't like. You can also save this page to your account.

Example 1
Project: ouroboros   Author: pybee   File: synchronize.py    (license) View Source Project 6 votes vote down vote up
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
# 
Example 2
Project: kbe_server   Author: xiaohaoppy   File: synchronize.py    (license) View Source Project 6 votes vote down vote up
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
# 
Example 3
Project: benchmarks   Author: tensorflow   File: cnn_util.py    (Apache License 2.0) View Source Project 5 votes vote down vote up
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) 
Example 4
Project: Learning-Concurrency-in-Python   Author: PacktPublishing   File: barriers.py    (license) View Source Project 5 votes vote down vote up
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") 
Example 5
Project: web_ctp   Author: molebot   File: test_multiprocessing.py    (license) View Source Project 5 votes vote down vote up
def setUp(self):
        self.barrier = self.Barrier(self.N, timeout=self.defaultTimeout) 
Example 6
Project: web_ctp   Author: molebot   File: test_multiprocessing.py    (license) View Source Project 5 votes vote down vote up
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 7
Project: web_ctp   Author: molebot   File: test_multiprocessing.py    (license) View Source Project 5 votes vote down vote up
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 8
Project: web_ctp   Author: molebot   File: test_multiprocessing.py    (license) View Source Project 5 votes vote down vote up
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 9
Project: web_ctp   Author: molebot   File: test_locks.py    (license) View Source Project 5 votes vote down vote up
def run_deadlock_avoidance_test(self, create_deadlock):
        NLOCKS = 10
        locks = [LockType(str(i)) for i in range(NLOCKS)]
        pairs = [(locks[i], locks[(i+1)%NLOCKS]) for i in range(NLOCKS)]
        if create_deadlock:
            NTHREADS = NLOCKS
        else:
            NTHREADS = NLOCKS - 1
        barrier = threading.Barrier(NTHREADS)
        results = []
        def _acquire(lock):
            """Try to acquire the lock. Return True on success, False on deadlock."""
            try:
                lock.acquire()
            except DeadlockError:
                return False
            else:
                return True
        def f():
            a, b = pairs.pop()
            ra = _acquire(a)
            barrier.wait()
            rb = _acquire(b)
            results.append((ra, rb))
            if rb:
                b.release()
            if ra:
                a.release()
        lock_tests.Bunch(f, NTHREADS).wait_for_finished()
        self.assertEqual(len(results), NTHREADS)
        return results 
Example 10
Project: stuff   Author: yaroslavvb   File: cnn_util.py    (license) View Source Project 5 votes vote down vote up
def log_fn(log):
  print(log)
  if FLAGS.flush_stdout:
    sys.stdout.flush()


# For Python 2.7 compatibility, we do not use threading.Barrier. 
Example 11
Project: stuff   Author: yaroslavvb   File: cnn_util.py    (license) View Source Project 5 votes vote down vote up
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) 
Example 12
Project: ouroboros   Author: pybee   File: _test_multiprocessing.py    (license) View Source Project 5 votes vote down vote up
def setUp(self):
        self.barrier = self.Barrier(self.N, timeout=self.defaultTimeout) 
Example 13
Project: ouroboros   Author: pybee   File: _test_multiprocessing.py    (license) View Source Project 5 votes vote down vote up
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 14
Project: ouroboros   Author: pybee   File: _test_multiprocessing.py    (license) View Source Project 5 votes vote down vote up
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 15
Project: ouroboros   Author: pybee   File: _test_multiprocessing.py    (license) View Source Project 5 votes vote down vote up
def test_single_thread(self):
        b = self.Barrier(1)
        b.wait()
        b.wait() 
Example 16
Project: ouroboros   Author: pybee   File: test_locks.py    (license) View Source Project 5 votes vote down vote up
def run_deadlock_avoidance_test(self, create_deadlock):
        NLOCKS = 10
        locks = [self.LockType(str(i)) for i in range(NLOCKS)]
        pairs = [(locks[i], locks[(i+1)%NLOCKS]) for i in range(NLOCKS)]
        if create_deadlock:
            NTHREADS = NLOCKS
        else:
            NTHREADS = NLOCKS - 1
        barrier = threading.Barrier(NTHREADS)
        results = []
        def _acquire(lock):
            """Try to acquire the lock. Return True on success, False on deadlock."""
            try:
                lock.acquire()
            except self.DeadlockError:
                return False
            else:
                return True
        def f():
            a, b = pairs.pop()
            ra = _acquire(a)
            barrier.wait()
            rb = _acquire(b)
            results.append((ra, rb))
            if rb:
                b.release()
            if ra:
                a.release()
        lock_tests.Bunch(f, NTHREADS).wait_for_finished()
        self.assertEqual(len(results), NTHREADS)
        return results 
Example 17
Project: retr   Author: aikipooh   File: farm.py    (license) View Source Project 5 votes vote down vote up
def __init__(self, num_threads, init, it, extendable=False, tn_tmpl=None,
                 reuse=None, handler=None):
        '''When extendable is True, it means that we need to leave threads ready in case the input list is extended (with extend()). Also the run() function can be invoked several times. The drawback is that underneath the input it is converted to a list, and then manipulated, so it's feasible to start with relatively small input data. Don't forget to call close(), it will clean up all the threads. Or you can use it as a context manager, so this will be called automatically upon __exit__().
If extendable is False, the code is simpler, but it supports iterables however big.
tn_tmpl is format() template with {} to be changed to thread's number.
 reuse tells whether to stash worked sessions for future reuse. If it's a list, it's a global list of warm sessions.
A function in the handler parameter is invoked each second, while the processing is postponed, so it shouldn't take long to complete'''
        self.waiting=0
        self.extendable=extendable

        if self.extendable:
            self.arr=list(it) # If it was a generator for example. Make it real.
            lg.info("Total: {}".format(len(self.arr)))
        else: # Otherwise leave it as it is, we'll treat it as iterator
            self.arr=iter(it)
            
        self.cond=Condition()
        self.num_threads=num_threads if num_threads else len(self.arr)
        # Barrier to wait on for the restart (when extendable)
        self.barr=Barrier(self.num_threads+1)
        self.init=init
        self.tn_tmpl=tn_tmpl
        self.reuse=reuse
        self.handler=handler
        # Objects living within threads, used to signal them to quit
        # (by setting quit_flag)
        self.objects=[]
        if type(reuse) is list:
            self.reuse_pool=reuse
        else:
            self.reuse_pool=None
            #if self.reuse: self.reuse_pool=[]
        
        self.q=Queue()
        
        self.tlist=[] 
Example 18
Project: kbe_server   Author: xiaohaoppy   File: _test_multiprocessing.py    (license) View Source Project 5 votes vote down vote up
def setUp(self):
        self.barrier = self.Barrier(self.N, timeout=self.defaultTimeout) 
Example 19
Project: kbe_server   Author: xiaohaoppy   File: _test_multiprocessing.py    (license) View Source Project 5 votes vote down vote up
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 20
Project: kbe_server   Author: xiaohaoppy   File: _test_multiprocessing.py    (license) View Source Project 5 votes vote down vote up
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 21
Project: kbe_server   Author: xiaohaoppy   File: _test_multiprocessing.py    (license) View Source Project 5 votes vote down vote up
def test_single_thread(self):
        b = self.Barrier(1)
        b.wait()
        b.wait() 
Example 22
Project: kbe_server   Author: xiaohaoppy   File: test_locks.py    (license) View Source Project 5 votes vote down vote up
def run_deadlock_avoidance_test(self, create_deadlock):
        NLOCKS = 10
        locks = [self.LockType(str(i)) for i in range(NLOCKS)]
        pairs = [(locks[i], locks[(i+1)%NLOCKS]) for i in range(NLOCKS)]
        if create_deadlock:
            NTHREADS = NLOCKS
        else:
            NTHREADS = NLOCKS - 1
        barrier = threading.Barrier(NTHREADS)
        results = []
        def _acquire(lock):
            """Try to acquire the lock. Return True on success, False on deadlock."""
            try:
                lock.acquire()
            except self.DeadlockError:
                return False
            else:
                return True
        def f():
            a, b = pairs.pop()
            ra = _acquire(a)
            barrier.wait()
            rb = _acquire(b)
            results.append((ra, rb))
            if rb:
                b.release()
            if ra:
                a.release()
        lock_tests.Bunch(f, NTHREADS).wait_for_finished()
        self.assertEqual(len(results), NTHREADS)
        return results 
Example 23
Project: Concurrency-With-Python   Author: elliotforbes   File: barriers.py    (license) View Source Project 5 votes vote down vote up
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") 
Example 24
Project: RibbaPi   Author: stahlfabrik   File: abstract_animation.py    (license) View Source Project 5 votes vote down vote up
def run(self):
        """This is the run method from threading.Thread"""
        #TODO threading.Barrier to sync with ribbapi
        #print("Starting")

        self.started = time.time()
        self._running = True
        self.animate()

    # def start(self): 
Example 25
Project: web_ctp   Author: molebot   File: test_multiprocessing.py    (license) View Source Project 4 votes vote down vote up
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
Project: GoPiGo3   Author: DexterInd   File: robot.py    (license) View Source Project 4 votes vote down vote up
def Main(trigger):
    print("   _____       _____ _  _____         ____  ")
    print("  / ____|     |  __ (_)/ ____|       |___ \ ")
    print(" | |  __  ___ | |__) || |  __  ___     __) |")
    print(" | | |_ |/ _ \|  ___/ | | |_ |/ _ \   |__ < ")
    print(" | |__| | (_) | |   | | |__| | (_) |  ___) |")
    print("  \_____|\___/|_|   |_|\_____|\___/  |____/ ")
    print("                                            ")

    print("Let your GoPiGo3 move around and avoid any obstacles.")
    print("Pay attention to how your GoPiGo3 moves around.")
    print("Avoid sharp corners / edges as the algorithm wasn't made for advanced stuff.")

    # Event object for letting one thread
    # control the other thread's flow control
    put_on_hold = threading.Event()
    # used for synchronizing the threads
    simultaneous_launcher = threading.Barrier(2)
    # for exchanging messages between threads
    sensor_queue = queue.Queue()

    print("\nWaiting threads to fire up")

    path_finder = threading.Thread(target=obstacleFinder, args=(trigger, put_on_hold, simultaneous_launcher, sensor_queue))
    controller = threading.Thread(target=robotController, args=(trigger, put_on_hold, simultaneous_launcher, sensor_queue))

    # start the threads
    path_finder.start()
    controller.start()

    # wait for the user to press CTRL-C
    # or to have an error while firing up the threads
    while not trigger.is_set() and not simultaneous_launcher.broken:
        sleep(0.001)
    # if an error was encountered
    if simultaneous_launcher.broken:
        # then exit the script
        sys.exit(1)

    # otherwise, just wait for the threads to finish
    path_finder.join()
    controller.join()

    # and then exit successfully
    sys.exit(0) 
Example 27
Project: ouroboros   Author: pybee   File: _test_multiprocessing.py    (license) View Source Project 4 votes vote down vote up
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 28
Project: kbe_server   Author: xiaohaoppy   File: _test_multiprocessing.py    (license) View Source Project 4 votes vote down vote up
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.