directory

  • A brief introduction to Python thread semaphore
  • Python thread semaphore principle
  • Semaphore functions for Python threads
  • Python thread semaphore use
  • Five. Guess you like it

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

A brief introduction to Python thread semaphore

From the thread threading module, you have learned how to use the lock/event/condition/timer module. Executing multiple threads at the same time can improve the efficiency of a program. But the more threads you have, the better. Maybe on a computer, it doesn’t matter if you run 20 or 30 threads directly. What if you run thousands or even tens of thousands? I’m sure your computer will crash…

Python thread semaphore principle

Threads run at the same time, can improve the operation efficiency of the program, but not the more threads, the better, and semaphore semaphore can be control by the built-in counter running at the same time the number of threads, starting a thread * * semaphore (consumption) built-in counter automatically minus one, thread ends (release semaphore) * * add a built-in counter automatically; The built-in counter is zero, and the starting thread blocks until either this thread or another thread terminates;

Semaphore functions for Python threads

  • Acquire ** — Consume semaphore, built-in counter reduced by one;
  • **release ** — Release semaphore, built-in counter increment;

Semaphore semaphore has a built-in counter that controls the number of threads. If acquire consumes the semaphore, the counter is automatically reduced by one. Release will release the semaphore, the counter will automatically increment one; When the counter is zero, the Acquire call is blocked until release releases the semaphore.

Python thread semaphore use

Example code for creating multiple threads with a limit of 5 threads running at a time:

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python thread semaphore. 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 time from thread # import time from thread # Semaphore = threading.semaphore (5) def foo(): semaphore = threading.semaphore (5) def foo(): Semaphore. Acquire () # acquire() # print() ",time.ctime()) # print the current system time semaphore.release() # release the lock if __name__ == "__main__": thread_list= list() for i in range(20): T = threading. Thread (target = foo, args ()) = # create a Thread thread_list. Append (t) t.s tart (#) starting a Thread for t thread_list in: T.join () print(" Program finished!" Tue May 4 12:01:43 2021 Tue May 4 12:01:43 2021 Tue May 4 12:01:43 2021 Tue May 4 12:01:43 2021 current time: Tue May 4 12:01:43 2021 Current time: Tue May 4 12:01:45 2021 Current time: Tue May 4 12:01:45 2021 Current time: Tue May 4 12:01:45 2021 Tue May 4 12:01:45 2021 Tue May 4 12:01:45 2021 Tue May 4 12:01:47 2021 current time: Tue May 4 12:01:47 2021 Current time: Tue May 4 12:01:47 2021 Tue May 4 12:01:49 2021 current time: Tue May 4 12:01:49 2021 Current time: Tue May 4 12:01:49 2021 Current time: Tue May 4 12:01:49 2021 Program ends! Process finished with exit code 0 '''Copy the code

According to the printed log, there are only 5 threads running at the same time. After an interval of two seconds, 5 threads will be started again until all the 20 threads are finished. If you do not set the semaphore, create a thread directly start, output time is all the same, this problem is relatively simple, you can go to experiment!

Five.Guess you like

  1. Python conditional derivations
  2. Python list derivations
  3. Python dictionary derivations
  4. Python function declarations and calls
  5. Python variable argument *argc/**kargcs
  6. Python anonymous function lambda
  7. Python return logic determines expressions
  8. Python string/list/tuple/dictionary conversions
  9. Python local and global variables
  10. The Python type function is different from the isinstance function
  11. Python is differs from ==
  12. Python mutable and immutable data types
  13. Shallow and deep copies of Python
  14. Read and write Python files
  15. Python exception Handling
  16. Python module import
  17. Python __name__ == ‘__main__’ explained in detail

Python thread semaphore

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