Barrier Objects in Python with Example
In this tutorial, we will learn what are barrier objects and how to create them in Python. We will also see an example of how to use them in Python.
Barrier Objects in Python
Barrier objects are used to provide a barrier for the execution of threads. When we create a barrier object, we specify a variable called ‘number_of_threads’. When a wait() function is called using this barrier object, the thread which called this function is going to wait until the number of threads which called the wait() function is equal to ‘number_of_threads’. Once it becomes equal all the threads are going to be released from the waiting state.
barrier_object = threading.Barrier(number_of_threads, action = None, timeout = None)
Initialize the barrier object by using threading.Barrier class as shown:
threading.Barrier(parties, action = None, timeout = None)
Parties: It denotes the number of threads that are waiting at the barrier.
Action: Denotes a function that can be executed by any thread that has been waiting for the barrier.
Timeout: Once this timeout value (specified in seconds) is attained by the barrier, it gets released from all the other waiting threads. It is by default considered none.
from threading import Barrier, Thread import time def producer (barrier_object): print ("Producer producing...") time.sleep(1) barrier_object.wait() print ("Produced\n") def consumer (barrier_object): print ("Waiting to consume...") barrier_object.wait() print ("Consumed") if __name__ == '__main__': barrier_object = Barrier(2) s = Thread (target = producer, args = (barrier_object,)) s.start() c = Thread (target = consumer, args = (barrier_object,)) c.start() s.join() c.join()
In the above code, we imported Barrier from threading for creating barrier object, Thread to create threads and time to call sleep() function.
In the main, let us create a barrier object with ‘number_of_threads’ as 2. We shall also create 2 threads targeting to functions producer and consumer. When thread ‘s’ starts executing producer, we called wait() function using barrier object. So, this thread will be waiting. When thread ‘c’ starts executing consumer, we called wait() function using barrier object. So, this thread will also be waiting. Now, the total number of threads waiting is equal to 2 i.e., ‘number_of_threads’. So, all the threads waiting are released and execution resumes.
Producer producing... Waiting to consume... Produced Consumed
Functions supported by the barrier class
- parties: It is equal to the number of threads that are required to reach the common barrier point.
- n_waiting: Number of threads waiting in the common barrier point for the barrier to be released.
- broken: This function returns True (a boolean-valued output) whenever the barrier enters the broken state.
- reset(): Set or return the barrier to the default state ( empty state ). And all the threads that are waiting for the barrier receive a BrokenBarrierError.
- wait(timeout=none): When the thread will have a timeout value equal to not none, it will be released from the barrier only when the timeout time is elapsed.
from random import randrange from threading import Barrier, Thread import time from time import ctime, sleep b = Barrier(6) names = ['First', 'Second', 'Third', 'Fourth', 'Fifth', 'Sixth'] def start(): name = names.pop() print('%s thread reached the barrier at %s \n' % (name, ctime())) time.sleep(2) b.wait() print('%s thread finished at %s \n' % (name, ctime())) threads =  print("Begin Execution") for i in range(6): threads.append(Thread(target=start)) threads[-1].start() for thread in threads: thread.join() print("Completed Execution")
Begin Execution Sixth thread reached the barrier at Sat Mar 14 19:02:08 2020 Fifth thread reached the barrier at Sat Mar 14 19:02:08 2020 Fourth thread reached the barrier at Sat Mar 14 19:02:08 2020 Third thread reached the barrier at Sat Mar 14 19:02:08 2020 Second thread reached the barrier at Sat Mar 14 19:02:08 2020 First thread reached the barrier at Sat Mar 14 19:02:08 2020 First thread finished at Sat Mar 14 19:02:10 2020 Fifth thread finished at Sat Mar 14 19:02:10 2020 Fourth thread finished at Sat Mar 14 19:02:10 2020 Third thread finished at Sat Mar 14 19:02:10 2020 Sixth thread finished at Sat Mar 14 19:02:10 2020 Second thread finished at Sat Mar 14 19:02:10 2020 Completed Execution