Python multi threaded programming (I) : overview of threading modules

  • 2020-05-05 11:25:47
  • OfStack

Python, an interpreted language, also has a dedicated threading model, and Python virtual machines use GIL (Global Interpreter Lock, the global interpreter lock) to mutually exclude threads from accessing Shared resources, but for the time being cannot take advantage of multiple processors. In Python, we mainly implemented thread and threading modules, Python threading module is some packaging for thread, which can be more convenient to be used, so we use threading module to achieve multi-threaded programming. This article focuses on Python's support for multithreaded programming.

At the language level, Python provides great support for multithreading, with easy support for creating threads, mutexes, semaphores, synchronization, and more. The following is the basic information and functions of the threading module on the website:

implementation module

thread: multithreading support module, generally not recommended;
threading: encapsulates thread, objectifying the operations of some threads.

threading module

The Thread thread class, which is one of the most commonly used classes, allows you to specify the execution of a thread function or to inherit from it.

The Timer is similar to the Thread, but waits a while before it starts running;
Lock lock primitive, which we can use when global variables are mutually exclusive;
RLock can reenter the lock, so that a single thread can acquire the lock again.
Condition condition variable, can make a thread stop, wait for other threads to meet a "condition";
Event general condition variable. Multiple threads can wait for an event to occur, after which all threads are activated.
Semaphore provides a "waiting room" structure for threads waiting for a lock.
BoundedSemaphore is similar to semaphore, but is not allowed to exceed the initial value.
Queue: implements multi-producer (Producer), multi-consumer (Consumer) queues, supports lock primitives, and provides good synchronization support between multiple threads.

Thread class

Is your main thread class, you can create process instances. The functions provided by this class include:
getName(self) returns the name of the thread
isAlive (self) Boolean flag indicating whether the thread is still running
isDaemon(self) returns the daemon flag
of the thread The join(self, timeout=None) program hangs until the thread ends, blocking timeout for up to 110en seconds
if timeout is given run(self) defines the function
for threads setDaemon(self, daemonic) sets the daemon flag of the thread to daemonic
setName(self, name) sets the name of the thread
start(self) starts thread execution

Queue provides the class

Queue queue
LifoQueue last in first out (LIFO) queue
PriorityQueue priority queue

then

In the next series of articles, we will use an example to demonstrate the various functions of threading, including but not limited to: threading in two ways, important functions of threading.Thread class, implementing reentrant locking using Lock mutex and RLock, implementing producer and consumer model using Condition, and multithreaded communication using Event and Semaphore.


Related articles: