Python's explanatory language also has a dedicated threading model, and Python virtual machines use the Gil (Global interpreter lock, globe interpreter Lock) to mutually exclusive threads access to shared resources, but are temporarily unable to take advantage of multiprocessor benefits. In Python we are mainly through the thread and threading these two modules, wherein the Python threading module is to do some packaging thread, can be more convenient to use, so we use The threading module implements multithreaded programming. This article mainly looks at Python's support for multithreaded programming.
At the language level, Python provides very good support for multithreading, allowing you to easily support the creation of threads, mutexes, semaphores, and synchronization. The following is the official online Introduction threading module basic information and functions:
Implementation module
Thread: Multithreading of the underlying support module, generally not recommended to use;
Threading: Thread is encapsulated, and some threads are manipulated.
Threading Module
Thread threading class, this is the most we use a class, you can specify the thread function execution or inherit from it can implement sub-threading function;
A timer is similar to a thread, but waits for a period of time before it starts to run;
Lock the primitive, which we can use when the global variable is mutually exclusive;
Rlock can re-enter the lock, so that the single-threaded can obtain the acquired lock again;
Condition A conditional variable that allows a thread to stop waiting for another thread to satisfy a certain "condition";
The condition variable that is common to the Event. Multiple threads can wait for an event to occur, and all threads are activated after the event occurs;
Semaphore provides a structure similar to the "Waiting Room" for the thread that waits for the 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 across multiple threads.
Thread class
Is your primary threading class and you can create process instances. The functions provided by this class include:
GetName (self) Returns the name of the thread
The IsAlive (self) Boolean flag that indicates whether the thread is still running
Isdaemon (self) Returns the daemon flag for the thread
The Join (self, timeout=none) program hangs until the thread ends and, if a timeout is given, blocks timeout seconds
Run (self) defines the function function of the thread
Setdaemon (self, daemonic) sets the thread's daemon flag to Daemonic
SetName (self, name) sets the name of the thread
Start (self) starts thread execution
Queue-Provided classes
Queue queues
Lifoqueue after in first out (LIFO) queue
Priorityqueue Priority Queue
Next
The next series of articles will use an example to illustrate the various features of threading, including but not limited to: two ways to Thread, threading. The important functions of the thread class, the use of lock mutex and rlock to implement a re-entry lock, the use of condition to implement producer and consumer models, the use of event and semaphore multi-threaded communication.