Sunday 20 December 2015

Python: threading: Barriers

Barrier is a synchronization aid that allows a set of threads to wait for each other to reach a common barrier point.  All thread must wait at that point, until all the threads reach this point.

‘threading.Barrier’ class is used to define Barrier object.

threading.Barrier(parties, action=None, timeout=None)
parties: Number Of Threads
action: called by one of the threads when they are released.
timeout : It is the default timeout value. If none is specified for the wait() method, this timeout value is used.

Following are the methods supported by Barrier class.

Method
Description
wait(timeout=none)
Pass the barrier. When all the threads party to the barrier have called this function, they are all released simultaneously. If a timeout is provided, it is used in preference to any that was supplied to the class constructor.

If an action was provided to the constructor, one of the threads will have called it prior to being released. If the call times out, the barrier is put into the broken state.
reset()
Return the barrier to the default, empty state. Any threads waiting on it will receive the BrokenBarrierError exception.
abort()
Put the barrier into a broken state. This causes any active or future calls to wait() to fail with the BrokenBarrierError.
parties
The number of threads required to pass the barrier.
n_waiting
The number of threads currently waiting in the barrier.
boken
A boolean that is True if the barrier is in the broken state.


MyThread.py
import threading
import time

class MyThread(threading.Thread):
 global event
 
 def run(self):
  global barrier
  print(threading.current_thread().getName(), " in process")
  time.sleep(2)
  barrier.wait()
  print(threading.current_thread().getName(), " finished")  
   
 
global barrier
barrier=threading.Barrier(3)
 
thread1 = MyThread(name="thread_1")
thread2 = MyThread(name="thread_2")
thread3 = MyThread(name="thread_3")
thread4 = MyThread(name="thread_4")
thread5 = MyThread(name="thread_5")

thread1.start()
thread2.start()
thread3.start()
thread4.start()
thread5.start()


Above program completes execution of three threads, remaining 2 threads wait for another one thread to join the party.
$ python3 MyThread.py 
thread_1  in process
thread_2  in process
thread_3  in process
thread_4  in process
thread_5  in process
thread_2  finished
thread_1  finished
thread_3  finished
^Z
[3]+  Stopped                 python3 MyThread.py


Update the program by adding another thread like below.

MyThread.py

import threading
import time

class MyThread(threading.Thread):
 global event
 
 def run(self):
  global barrier
  print(threading.current_thread().getName(), " in process")
  time.sleep(2)
  barrier.wait()
  print(threading.current_thread().getName(), " finished")  
   
 
global barrier
barrier=threading.Barrier(3)
 
thread1 = MyThread(name="thread_1")
thread2 = MyThread(name="thread_2")
thread3 = MyThread(name="thread_3")
thread4 = MyThread(name="thread_4")
thread5 = MyThread(name="thread_5")
thread6 = MyThread(name="thread_6")

thread1.start()
thread2.start()
thread3.start()
thread4.start()
thread5.start()
thread6.start()

$ python3 MyThread.py 
thread_1  in process
thread_2  in process
thread_3  in process
thread_4  in process
thread_5  in process
thread_6  in process
thread_5  finished
thread_2  finished
thread_1  finished
thread_6  finished
thread_4  finished
thread_3  finished




Previous                                                 Next                                                 Home

No comments:

Post a Comment