+

Barrier Objects in Python

Barriers can even be used to synchronize access between threads. Typically, however, a barrier is used to pool the output of streams. The barrier object can be reused for the same number of threads for which it was originally initialized.

Barrier initialization

The barrier can be initialized using the Threading.Barrier class as shown in the program below. The number in brackets represents the number of threads that the barrier should wait.

Syntax :

  barrier = threading.Barrier (number_of_threads, action = None, timeout = None)  

Create a barrier object for number_of_threads. An action, if provided, is called to be called by one of the threads when they are released. timeout is the default timeout value if no wait () method is specified.

import threading

 

barrier = threading.Barrier ( 3 )

 

class thread ( threading.Thread):

def __init __ ( self , thread_ID):

  threading.Thread .__ init __ ( self )

self . thread_ID = thread_ID

  def run ( self ):

print ( str ( self . thread_ID) + " " )

  barrier.wait ()

 

thread1 = thread ( 100 )

thread2 = thread ( 101 )

 
thread1.start ( )
thread2.start ()
barrier.wait ()

 

print ( "Exit" )

Exit :

  100 101 Exit  

Some general function calls related to multithreading. Barrier class:

  1. Checking the state of the barrier:
    Violated: Boolean True if the barrier is in a collapsed state. 
    Syntax :
    barrier.broken

  2. sides : the number of threads required to pass the barrier. 
    Syntax :
    barrier.parties
  3. Barrier canceling:
    abort: put the barrier in a broken state. This causes any active or future wait () calls to fail with BrokenBarrierError

    Interrupting a function call on a barrier is often required to skip deadlock conditions during program execution. 
    Syntax :

    barrier.abort()
  4. Reset barrier:
    reset: Return the barrier to its default, empty state. Any threads waiting for it will receive a BrokenBarrierError.

    Syntax :

    barrier.reset()
  5. wait : go through the barrier. When all parties involved in the barrier have called this function, they are all freed at the same time. If a timeout is specified, it is used in place of any supplied to the class constructor.

    The return value is an integer ranging from 0 to sides — 1, different for each stream. If the waiting time has expired, the barrier is transferred to the violated state. This method can throw a BrokenBarrierError if the barrier is broken or dropped while waiting for a thread.

    Syntax :

      barrier.wait (timeout = None )  
  6. n_waiting : The number of threads currently waiting in the barrier. 
    Syntax :
    barrier.n_waiting

Usually when a barrier object is dropped or destroyed, a BrokenBarrierError exception is thrown.

Here is a sample program to show how barriers are used in Python

# program for demonstration
# barriers in python

 

import threading

 

barrier = threading.Barrier ( 3 )

 

class thread (threading.Thread):

def __ init __ ( self , thread_ID):

threading.Thread .__ init __ ( self )

self . thread_ID = thread_ID

def run ( self ):

  print ( str ( self . thread_ID) + " " )

  print ( "Parties =" + str (barrier.parties) + "" )

print ( "n_waiting =" + str (barrier.n_waiting) + "" )

  barrier.wait ()

 

thread1 = thread ( 100 )

thread2 = thread ( 101 )

 
thread1.start ()
thread2.start ()

 
barrier.wait ()

 

print ( str (barrier.broken) + "" )

barrier.reset ()

print ( "n_waiting after reset =" + str ( barrier.n_waiting))

barrier.abort ()

print ( "End" )

Exit:

 100 101 Parties = 3 Parties = 3 n_waiting = 1 n_waiting = 1 False n_waiting after reset = 0 End 

This article courtesy of Mayank Kumar . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you`d like to share more information on the topic discussed above.

Get Solution for free from DataCamp guru