directory

  • Python thread barriers
  • The Barrier principle for Python threads
  • Barrier functions for Python thread barriers
  • Python thread barriers
    • 1. Case 1: Routine use
    • 2. Case 2: Reset the number of thread barriers
  • Five. Guess you like it

Recommended path for learning Python: Python Learning Directory >> Python Basics

Python thread barriers are also called barriers.

Python thread barriers

Parties -- A thread counter that tracks the number of threads, also known as threading obstacles; Action - is a callable function. When the waiting thread reaches the thread-barrier parties, one of the threads will call the corresponding function of the action first and then execute the thread's own internal code; Timeout - The default timeout period; Barrier = threading.Barrier(parties, action=None, timeout=None)Copy the code

The Barrier principle for Python threads

Different from the mutex Lock, Event, Timer, etc., a multi-threaded Barrier sets the number of threads to a Barrier called parties, which blocks all threads if the number of waiting threads does not reach the number of parties. When the number of waiting threads reaches this number, all waiting threads are woken up.

This may sound a bit abstract, but take the player as an example: A thread do first player initialization (loading a local file or for broadcast address), then a thread for video images, a thread to obtain video voice, only when the initialization is completed, the video images to obtain complete, video voice get finished, the player will start playing, any of a thread is not completed, The player will block until all three tasks are complete!

Barrier functions for Python thread barriers

Wait (timeout=None) — Block and attempt to pass an obstacle. Parties perform the corresponding action function and execute the internal code; otherwise, continue to wait.

If wait(timeout=None) times out, the barrier enters the open state! This method raises a BrokenBarrierError if the barrier is broken or reset during a thread wait. Note the addition of exception handling.

Reset – resets the number of thread barriers, returning the default null state, that is, the currently blocked thread restarts. This method raises a BrokenBarrierError if the barrier is broken or reset while the thread is waiting.

Python thread barriers

1. Case 1: Routine use

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python thread Barrier object Barrier. Py @time :2021/05/04 07:37 @motto: A thousand miles without a small step, a river without a small stream, the wonderful life of the program needs to be accumulated with perseverance! "" # import thread module import threading def plyer_display(): print(' Initialization through completion, audio/video synchronization complete, can start playing.... Barrier = threading.barrier (3, action=plyer_display, timeout=None) def player_init(statu): Wait (2) except Exception as e: print(statu) try: Print (" Wait time out... ") else: print("xxxooooxxxxxooooxxxoooo") if __name__ == '__main__': Statu_list = ["init ready","video ready","audio ready"] thread_list = list() for I in range(0,3): t = threading.Thread(target=player_init,args=(statu_list[i],)) t.start() thread_list.append(t) for t in thread_list: Init ready video ready Audio Ready After initialization, audio and video synchronization is complete, you can start playing.... xxxooooxxxxxooooxxxoooo xxxooooxxxxxooooxxxoooo xxxooooxxxxxooooxxxoooo '''Copy the code

Barrier. Wait (timeout=None) if barrier. Wait (timeout=None) causes a BrokenBarrierError.

2. Case 2: Reset the number of thread barriers

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python thread Barrier object Barrier. Py @time :2021/05/04 07:37 @motto: A thousand miles without a small step, a river without a small stream, the wonderful life of the program needs to be accumulated with perseverance! "" # import thread module import threading def plyer_display(): print(' Initialization through completion, audio/video synchronization complete, can start playing.... Barrier = threading.barrier (3, action=plyer_display, timeout=None) def player_init(statu): Wait (2) except Exception as e: print(statu) try: # print(" BrokenBarrierError ") ") continue else: print("xxxooyyyxxxooyyyxxxooyyy") break if __name__ == '__main__': Statu_list = ["init ready","video ready","audio ready"] thread_list = list() for I in range(0,3): t = threading.Thread(target=player_init,args=(statu_list[i],)) t.start() thread_list.append(t) if i == 1: Print (" Don't want to see a romantic movie, I want to see a romantic action movie...." ) barrier. Reset () for t in thread_list: t.join() "init ready video Ready Don't want to watch romance.... Init Ready Video Ready Audio Ready After initialization, audio and video synchronization is complete, you can start playing.... xxxooyyyxxxooyyyxxxooyyy xxxooyyyxxxooyyyxxxooyyy xxxooyyyxxxooyyyxxxooyyy '''Copy the code

Barrier. Wait (timeout=None) if barrier. Wait (timeout=None) causes a BrokenBarrierError.

Five.Guess you like

  1. Python conditional derivations
  2. Python list derivations
  3. Python dictionary derivations
  4. Python variable argument *argc/**kargcs
  5. Python anonymous function lambda
  6. Python return logic determines expressions
  7. Python is differs from ==
  8. Python mutable and immutable data types
  9. Shallow and deep copies of Python
  10. Python exception Handling
  11. Python thread creation and parameter passing
  12. Python thread mutex Lock
  13. Python thread time Event
  14. The Python thread Condition variable Condition
  15. Python thread Timer Timer
  16. Python thread Semaphore
  17. Python thread Barrier object Barrier
  18. Python thread Queue Queue – FIFO
  19. Python thread queue LifoQueue – LIFO
  20. Python thread PriorityQueue PriorityQueue
  21. Python thread Pool ThreadPoolExecutor
  22. Python thread Pool ThreadPoolExecutor
  23. The Python Process module
  24. The Python Process Process is different from threading
  25. Python interprocess communication Queue/Pipe
  26. Python process Pool multiprocessing.pool
  27. Python GIL lock

Python is a Barrier to threading

This article is published by the blog – Ape Say Programming Ape Say programming!