Course Content
Python Programming Tutorial
About Lesson

Barrier Object – Python Multithreading

Barrier object is created by using Barrier class which is available in the threading module. This object can be used where we want a set of threads to wait for each other.

For example, if we have two threads and we want both the threads to execute when both are ready. In such situation both the threads will call the wait() method on the barrier object once they are ready and both the threads will be released simultaneously only when both of them have called the wait() method.

Following is the syntax of the Barrier class to initialize a barrier object:

threading.Barrier(parties, action=None, timeout=None)
 

Where, parties specifies the number of threads waiting at the barrier, action can be used to specify a function which will be executed by any one thread waiting for the barrier and timeout is used to specify a timeout value in seconds after which the barrier will be released from all the waiting threads.


Functions provided by Barrier class

Following are the function provided by the Barrier class:

wait(timeout=None) method

When we want a set of threads to wait for each other, we initialise the barrier object with the number of threads specified as parties parameter while barrier object creation.

Then, the barrier will be released only when the same number of threads call the wait() method of the barrier object.

 

If in a thread we provide the timeout value while calling the wait() method, then that thread will get released from the barrier once the timeout time is passed.

This method returns an integer value from 0 to parties-1. We can use this value to identify which all threads have reached the waiting point of the barrier and which all are still not there, for example:

i = barrier.wait()
if i == 0:
    # Only one thread Will be allowed to print this
    print("0th thread passed the barrier")
 

The return value of the wait() method can be used for carrying out some clean up task for example if we have 3 threads doing some work and using a temporary file to store data, once the threads are done, we can put a check on the wait() method that when the last thread reaches its waiting point and the barrier is to be released, before that delete the file.

i = barrier.wait()
if i == 2:
    # delete the temporay file
 

If the wait call times out, the barrier is put into the broken state.

The wait() method may raise a BrokenBarrierError if the barrier breaks or resets as a thread waits.

reset() method

This function resets the barrier to its default, empty state. If there are threads waiting for the barrier to get released will receive BrokenBarrierError.

abort() method

This method when called on a barrier puts it into the broken state. Once this method is called by any of the waiting thread, the rest of the threads waiting for the barrier to be released will receive BrokenBarrierError.

 

We may want to use this method in case of some deadlock situation to release the waiting threads.

parties

This returns the number of threads we need to pass the barrier.

n_waiting

This returns the number of threads that currently are waiting for the barrier to be released.

broken

This is a Boolean value that is True if the barrier is in the broken state.


Time for an Example!

Below we have a simple example where we have two threads representing server and client where in the client thread will wait for the server to get ready before sending any request to it.

In the above code, try calling the abort() method in either client or server and see what happens. You might have to use the try block and catch the BrokenBarrierError in the code.

error: Content is protected !!