Python's interpretive language also has a dedicated threading model, with the Python virtual machine using Gil (Global interpreter lock, the globe interpreter Lock) to mutually exclusive threads accessing shared resources, but temporarily unable to take advantage of multiprocessor advantages. In Python we do this mainly through thread and threading two modules, where Python's threading modules are packaged for thread and can be used more conveniently, so we use Threading module to achieve multithreaded programming. In this article, we'll look at Python's support for multithreaded programming.
At the language level, Python provides a good support for multithreading, and can easily support the creation of threads, mutexes, semaphores, synchronization and other features. The following is the official Website Introduction Threading Module basic information and function:
Implementation module
Thread: Multithreading of the underlying support module, generally do not recommend the use;
Threading: Thread is encapsulated to manipulate the operations of some threads.
Threading Module
Thread threads class, this is the most we use a class, you can specify the thread function execution or inherit from it can implement the child threading function;
The timer is similar to thread, but waits for some time before it starts to run;
Lock Primitives, which we can use when we are mutually exclusive to global variables;
The rlock can be locked so that the single thread can regain the acquired lock again;
Condition condition variable, can let a thread stop, wait for other threads to meet a "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;
The semaphore provides a structure similar to a "waiting room" for the thread waiting for the lock;
The Boundedsemaphore is similar to the semaphore, but does not allow the initial value to be exceeded;
Queue: Implements multiple producer (Producer), multiple consumer (Consumer) queues, supports the lock primitives, and provides good synchronization support across multiple threads.
Thread class
Is your main thread class, you can create a process instance. The functions provided by this class include:
GetName (self) Returns the name of the thread
IsAlive (self) Boolean flag indicating whether this thread is still running
Isdaemon (self) Returns the daemon flag of a thread
The Join (self, timeout=none) program hangs until the thread finishes and, if timeout is given, blocks timeout seconds
Run (self) defines the function function of a thread
Setdaemon (self, daemonic) sets the thread's daemon flag to Daemonic
SetName (self, name) sets the name of the thread
Start thread execution starting (self)
The class provided by the queue
Queue queues
Lifoqueue back in First Out (LIFO) queue
Priorityqueue Priority Queue
Next
The next series of articles will use an example to show the threading features, including but not limited to: two ways of threading, threading. Important functions of the thread class, use lock mutexes and Rlock to implement the lock, use condition to implement the producer and consumer models, use event and semaphore multithreading communication.