If you read my previous article carefully , You'll find that in multithreading synchronization , It has been introduced that locking and event objects can be synchronized , Next, let's introduce a different synchronization object : barrier (threading.Barrier). The barrier object sets a gate in multithreading synchronization , And this door has a lock , It takes a certain number of threads to arrive before it can be unlocked . If the first thread arrives , Will wait , Until the quantity is satisfied . It's like organizing a tour , All tourists will drive when they arrive at the bus , Not because a few people get on the bus first , Let the bus go . The incident of everyone arriving at the bus , It's called a barrier event .
Barrier(parties, action=None, timeout=None)
Each thread calls wait() Try to get through the obstacles , And block , Until the number of blocks reaches parties when , All blocked threads are released at the same time .
action Is a callable object , When the thread is released , One of the threads will first call action, Then run your own code .
timeout The default timeout is .
Method :
wait(timeout=None)
Try to get through obstacles and block .
The return value is a value in 0 To parties-1 Range of integers , Every thread is different .