Some developers explicitly instruct the Python Virtual Machine to create a new thread, so that Python can realize that users need multi-threaded support. Without such support, it is unknown whether the Python virtual machine can develop smoothly.
Among the interfaces provided by the thread module of the Python virtual machine, it must not be the interface 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 on the century:
- [thread_nt.h]
-
- int PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
-
- {
-
- int success ;
-
- success = aLock && EnterNonRecursiveMutex((PNRMUTEX) aLock, (waitflag
-
- == 1 ? INFINITE : 0)) == WAIT_OBJECT_0 ;
-
- return success;
-
- }
-
- DWORD EnterNonRecursiveMutex(PNRMUTEX mutex, BOOL wait)
-
- {
-
- /* Assume that the thread waits successfully */
-
- DWORD ret;
-
- /* InterlockedIncrement(&mutex->owned) == 0 means that no thread
-
- currently owns the mutex */
-
- if (!wait)
-
- {
-
- if (InterlockedCompareExchange((PVOID *)&mutex->owned, (PVOID)0,
-
- (PVOID)-1) != (PVOID)-1)
-
- return WAIT_TIMEOUT ;
-
- ret = WAIT_OBJECT_0 ;
-
- }
-
- else
-
- ret = InterlockedIncrement(&mutex->owned) ?
-
- /* Some thread owns the mutex, let's wait... */
-
- WaitForSingleObject(mutex->hevent, INFINITE) : WAIT_OBJECT_0 ;
-
- mutex->thread_id = GetCurrentThreadId() ; /* We own it */
-
- return ret ;
-
- }
In the code list, 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.
Multi-threaded support is not at no cost. The simplest point is that if the multithreading mechanism is activated and the executed Python program does not have multithreading, the Python virtual machine will also activate Thread Scheduling after the first 100 commands.
If multithreading is not activated, the Python virtual machine does not have to do this useless work. Therefore, Python selects a policy that allows users to activate the multithreading mechanism. When a Python virtual machine is started, the multi-thread mechanism is not activated. It only supports a single thread. Once the user calls thread. start_new_thread.
Explicitly instruct the Python Virtual Machine to create a new thread so that Python can realize that users need multi-thread support. At this time, python virtual opportunity Automatically Establishes the data structure, environment, and the crucial GIL required by the multithreading mechanism.
Here, we finally see the platform relevance of the multithreading mechanism in Python. Under the Python25 \ Python directory, there are a large number of files such as thread _ ***. h. These files wrap the native threads of different operating systems and expose them to Python through a unified interface. For example, PyThread_allocate_lock is such an interface.
The thread_nt.h here is packaged with the native thread of the Win32 platform. In the code analysis later in this chapter, there will be a lot of platform-related code. We take the Win32 platform as an example. In PyThread_allocate_lock, similar to PyEval_InitThreads, it checks an initialized variable. If GIL indicates whether the Python multi-thread environment has been established.
The initialized variable indicates whether the initialization is complete to use the native thread provided by the underlying platform. These initialization actions are usually provided by the underlying operating system. Different Operating Systems may need different initialization actions.
- Introduction to Python system files
- How to correctly use Python Functions
- Detailed introduction and analysis of Python build tools
- Advantages of Python in PythonAndroid
- How to Use the Python module to parse the configuration file?