Python-multithreading, python-Multithreading
Python Multithreading
Python provides multiple modules to support multi-threaded programming, including thread, threading, and Queue modules. Programs can use the thread and threading modules to create and manage threads.
The thread module provides basic thread and lock support, while the threading module provides higher-level and more comprehensive thread management.
With the Queue module, you can create a Queue data structure for sharing among multiple threads.
In python3.0, the thread has been renamed as _ thread
The execution of Python code is controlled by the Python Virtual Machine (also known as the main loop of the interpreter.
Access to Python virtual machines is controlled by the global interpreter lock (GIL. This lock is used to ensure that only one thread can run at the same time. In a multi-threaded environment, the Python virtual machine is executed as described below.
1. Set GIL.
2. Switch to a thread to run.
3. Perform one of the following operations.
A. specify the number of bytecode commands.
B. The thread proactively gives control (time. sleep (0) can be called ).
4. Set the thread to sleep (switch out of the thread ).
5. Unlock GIL.
6. Repeat the preceding steps.
When a thread completes function execution, it exits. In addition, you can also call such as thread. exit () or sys. exit () and other standard methods to exit the Python process, or throw a SystemExit exception to exit the thread. However, you cannot directly "Terminate" a thread.
Platforms supported by python multithreading:
Most UNIX-like platforms (such as Linux, Solaris, Mac OS X, and * BSD) and Windows platforms.
Python uses POSIX-compatible threads, that isPthread, click to jump to Baidu encyclopedia
No thread is used
We will use the time. sleep () function to demonstrate how the thread works.
Create two time cycles: one sleep for 4 seconds (loop0 (), and the other sleep for 2 seconds (loop1 () (here "loop0" and "loop1" are used as function names, it implies that we will eventually have a circular sequence ).
import timedef loop0(): print("start loop 0 at: %s" % time.ctime()) time.sleep(4) print("loop 0 done at: %s" % time.ctime())def loop1(): print("start loop 1 at: %s" % time.ctime()) time.sleep(2) print("loop 1 done at: %s" % time.ctime())def main(): print("starting at: %s" % time.ctime()) loop0() loop1() print("all DONE at: %s" % time.ctime())if __name__ == '__main__': main()
Starting at: Tue Jan 23 16:03:21 2018 start loop 0 at: Tue Jan 23 16:03:21 2018 loop 0 done at: Tue Jan 23 16:03:25 2018 start loop 1: tue Jan 23 16:03:25 2018 loop 1 done at: Tue Jan 23 16:03:27 2018all DONE at: Tue Jan 23 16:03:27 2018 from the output, we can see that the output took us 7 seconds.
Output result of a single thread
_ Thread module-a non-recommended Module
_ Thread module and Lock Object
The core function of the thread module is start_new_thread (). Its Parameters include function (object), function parameters, and optional keyword parameters.
Start_new_thread () must contain the first two parameters. Therefore, even if the function to be executed does not require a parameter, an empty tuples must be passed.
Use the _ thread module for programming:
We only need to slightly modify the above Code.
Import timeimport _ threaddef loop0 (): print ("start loop 0 at: % s" % time. ctime () time. sleep (4) print ("loop 0 done at: % s" % time. ctime () def loop1 (): print ("start loop 1 at: % s" % time. ctime () time. sleep (2) print ("loop 1 done at: % s" % time. ctime () def main (): print ("starting at: % s" % time. ctime () _ thread. start_new_thread (loop0, () _ thread. start_new_thread (loop1, () time. sleep (5) # Pause for 5 seconds, because our loop0 function is suspended for 4 seconds. If we are less than 4 seconds, loop 0 done cannot be output... print ("all DONE at: % s" % time. ctime () if _ name _ = '_ main _': main ()
Starting at: Tue Jan 23 16:14:19 2018 start loop 0 at: Tue Jan 23 16:14:19 2018 start loop 1 at: Tue Jan 23 16:14:19 2018 loop 1 done: tue Jan 23 16:14:21 2018 loop 0 done at: Tue Jan 23 16:14:23 2018all DONE at: Tue Jan 23 16:14:24 2018 from the output result, we can see that loop0 and loop1 are both started, instead of executing loop0 first
_ Thread multi-thread output results