Virtual machine level
The Python virtual machine uses the Gil (Global interpreter lock, the Universal interpreter lock) to mutually exclusive threads access to shared resources, temporarily unable to take advantage of multiprocessor benefits.
Language level
At the language level, Python provides good support for multithreading, including: Thread,threading,queue, a multithreaded-related module in Python. It is easy to support the creation of threads, mutexes, semaphores, synchronization and other features.
Thread: The underlying support module for multithreading is generally not recommended for use.
Threading: Thread is encapsulated, and some threading operations are instantiated, providing the following classes:
Thread Threading Class
The timer is similar to thread, but waits a while before it starts to run
Lock the original language
Rlock locks can be re-entered. Enables a single thread to obtain a lock already acquired again
Condition A conditional variable that allows a thread to stop waiting for another thread to satisfy 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
Semaphore provides a "waiting room" structure for the thread that waits for the lock
Boundedsemaphore similar to semaphore but 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. The classes provided:
Queue queues
Lifoqueue after in first out (LIFO) queue
Priorityqueue Priority Queue
Where the thread class is your primary thread class, you can create a process instance. 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
Python's threading. The thread class has a run method that defines a function function for the thread that can be overridden in its own thread class. After creating your own thread instance, through the start method of the thread class, you can start the thread and hand it over to the Python virtual machine and call the Run method execution thread when the thread gets the chance to execute.
Import Threadingimport Timeclass MyThread (threading. Thread):d EF Run: For I in range (3): Time.sleep (1) msg = "I ' m" +self.name+ ' @ ' +str (i) Print Msgdef test (): For I in range (5): T = MyThread () t.start () if __name__== ' __main__ ': Test ()
Execution Result:
I ' m Thread-1 @ 0
I ' m Thread-2 @ 0
I ' m Thread-5 @ 0
I ' m Thread-3 @ 0
I ' m Thread-4 @ 0
I ' m Thread-3 @ 1
I ' m Thread-4 @ 1
I ' m Thread-5 @ 1
I ' m Thread-1 @ 1
I ' m Thread-2 @ 1
I ' m Thread-4 @ 2
I ' m Thread-5 @ 2
I ' m Thread-2 @ 2
I ' m Thread-1 @ 2
I ' m Thread-3 @ 2
From the code and execution results, we can see that the order of execution of multi-threaded routines is indeterminate. When executed to the sleep statement, the thread is blocked (Blocked), and after sleep ends, the thread enters the ready (Runnable) state and waits for dispatch. Thread scheduling will choose a thread to execute itself. The above code only guarantees that each thread runs the full run function, but the start order of the thread and the order of execution of each loop in the run function are not determined.
It is also important to note that:
1. Each thread must have a name, although the above example does not specify the name of the thread object, but Python automatically assigns a name to the thread.
2. When the thread's run () method ends, the thread finishes.
3. The thread scheduler cannot be controlled, but there are other ways in which thread scheduling can be affected.
The above example simply demonstrates the creation of threads, active suspend, and exit threads
Python multithreaded programming (1)