In Python, both processes and threads are operating systems. In this thread model, only one thread can exist in a process, and the remaining processes must wait until the current thread finishes running, the analysis is described in detail below.
Among the interfaces provided by the Python thread module, it must not be a few interfaces for creating threads. If this interface is not provided, what is the significance of life? J? In the above thread1.py, we created a brand new thread through the start_new_thread provided by it. Well, let's go to start_new _ thread to see how Python is working in the century.
- [Threadmodule. c]
-
- Static PyObject * thread_PyThread_start_new_thread (PyObject * self, PyObject
-
- * Fargs)
-
- {
-
- PyObject * func, * args ,*Keyw=NULL;
-
- Struct bootstate * boot;
-
- Long ident;
-
- PyArg_UnpackTuple (fargs, "start_new_thread", 2, 3, & func, & args,
-
- & Keyw );
-
- // [1]: Create a bootstate Structure
-
- Boot=PyMem_NEW(Struct bootstate, 1 );
-
- Boot->Interp=PyThreadState_GET()->Interp;
-
- Boot->FuncFunc= Func;
-
- Boot->ArgsArgs= Args;
-
- Boot->KeywKeyw= Keyw;
-
- // [2]: Initialize the multi-threaded Environment
-
- PyEval_InitThreads ();/* Start the interpreter's thread-awareness */
-
- // [3]: Create a thread
-
- Ident=PyThread_start_new_thread(T_bootstrap, (void *) boot );
-
- Return PyInt_FromLong (ident );
-
- }
[1], [2], and [3] of code listing 15-1 have the following meanings:
[1] Create and initialize the bootstate structure boot. In boot, all information about the thread will be saved, such as the thread process and thread process parameters.
[2] initialize the Python multi-threaded environment.
[3] Create a native thread of the operating system with boot as the parameter.
In [1] of code listing 15-1, we noticed that the Python PyInter-preterState object is saved in boot-> interp, which carries global information such as the Python module pool, all threads in Python share the global information. For the initialization action of the multi-threaded environment shown in [2] of code listing 15-1, it is worth noting that the multi-threaded environment is not supported when Python is started.
In other words, the data structures and GIL that support multithreading in Python are not created. This is because most Python programs do not require multithreading. If multiple threads appear in a Python script that simply calculates word frequency, we will be crazy about such code.
- Analysis of Python global variables in Python
- Easy to learn PythonWin Module
- How to Implement Python Technology in games
- Deep introduction to Python Interfaces
- Description of Python Extension
The support for multithreading is not costly. The simplest point is that if the multithreading mechanism is activated and the Python Program executed does not contain multithreading, after 100 instructions, the Python virtual machine also activates thread scheduling. If multithreading is not activated, virtual machines in the Python multi-threaded environment do not have to do this useless work. Therefore, Python selects a policy that allows users to activate the multithreading mechanism. When the Python virtual machine is started.
The multithreading mechanism is not activated. It only supports single threads. Once the user calls thread. start_new_thread, it explicitly instructs the Python Virtual Machine to create a new thread. Python can realize that users need multi-threaded support. At this time, the Python multi-threaded environment will automatically establish the data structure and environment required by the multi-threaded mechanism and the crucial GIL.