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.
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.
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.
100 101 Exit
Some general function calls related to multithreading. Barrier class:
Interrupting a function call on a barrier is often required to skip deadlock conditions during program execution.
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.
barrier.wait (timeout = None )
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
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.