How to innovate Python Virtual Machine threads

Source: Internet
Author: User

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:

 
 
  1. [thread_nt.h]  
  2.  
  3. int PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)  
  4.  
  5. {  
  6.  
  7.     int success ;  
  8.  
  9.     success = aLock && EnterNonRecursiveMutex((PNRMUTEX) aLock, (waitflag 
  10.  
  11.        == 1 ? INFINITE : 0)) == WAIT_OBJECT_0 ;  
  12.  
  13.     return success;  
  14.  
  15. }  
  16.  
  17. DWORD EnterNonRecursiveMutex(PNRMUTEX mutex, BOOL wait)  
  18.  
  19. {  
  20.  
  21.     /* Assume that the thread waits successfully */  
  22.  
  23.     DWORD ret;  
  24.  
  25.     /* InterlockedIncrement(&mutex->owned) == 0 means that no thread  
  26.  
  27.         currently owns the mutex */  
  28.  
  29.     if (!wait)  
  30.  
  31.     {  
  32.  
  33.         if (InterlockedCompareExchange((PVOID *)&mutex->owned, (PVOID)0,  
  34.  
  35.               (PVOID)-1) != (PVOID)-1)  
  36.  
  37.             return WAIT_TIMEOUT ;  
  38.  
  39.         ret = WAIT_OBJECT_0 ;  
  40.  
  41.     }  
  42.  
  43.     else  
  44.  
  45.         ret = InterlockedIncrement(&mutex->owned) ?  
  46.  
  47.             /* Some thread owns the mutex, let's wait... */  
  48.  
  49.             WaitForSingleObject(mutex->hevent, INFINITE) : WAIT_OBJECT_0 ;  
  50.  
  51.     mutex->thread_id = GetCurrentThreadId() ; /* We own it */  
  52.  
  53.     return ret ;  
  54.  
  55. }  

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.

  1. Introduction to Python system files
  2. How to correctly use Python Functions
  3. Detailed introduction and analysis of Python build tools
  4. Advantages of Python in PythonAndroid
  5. How to Use the Python module to parse the configuration file?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.