Analysis on Python Multithreading

Source: Internet
Author: User

The establishment of a multi-threaded Python environment is straightforward, mainly the creation of GIL. We already know the significance of GIL for the multi-threaded mechanism of Python. But how is GIL implemented? Well, this is still a very troublesome problem. 

 
 
  1.  
  2. PNRMUTEX AllocNonRecursiveMutex(void)  
  3.  
  4. {  
  5.  
  6.     PNRMUTEX mutex = (PNRMUTEX)malloc(sizeof(NRMUTEX)) ;  
  7.  
  8.     if(mutex && !InitializeNonRecursiveMutex(mutex)) {  
  9.  
  10.             free(mutex);  
  11.  
  12.             Mutex = NULL;  
  13.  
  14.     }  
  15.  
  16.     return mutex ;  
  17.  
  18. }  
  19.  
  20. BOOL InitializeNonRecursiveMutex(PNRMUTEX mutex)  
  21.  
  22. {  
  23.  
  24.     ……  
  25.  
  26.     mutex->owned = -1 ;  /* No threads have entered NonRecursiveMutex */  
  27.  
  28.     mutex->thread_id = 0 ;  
  29.  
  30.     mutex->hevent = CreateEvent(NULL, FALSE, FALSE, NULL) ;  
  31.  
  32.     return mutex->hevent != NULL ;  /* TRUE if the mutex is created */  
  33.  
  34. }  

I finally saw the mysterious GILinterpreter_lock). I never expected it, but it actually showed a simple void *. But in another thought, in C, void * can be almost anything. This guy is a omnipotent container.

As you can see, no matter how many threads are created, the action of creating a multi-threaded environment in Python is only executed once. At the beginning of PyEval_InitThreads, Python checks whether GIL has been created. If yes, no action is performed. Otherwise, the GIL is created. The creation of GIL is completed by PyThread_allocate_lock. Let's take a look at what the GIL is.

Here, we finally see the platform relevance of the Python multithreading mechanism. Under the Python25 \ Python directory, there are a large number of files such as thread. In these files, native threads of different operating systems are encapsulated and exposed to Python through a unified interface. For example, PyThread_allocate_lock here is such an interface.

  • How to Implement Python Technology in games
  • Deep introduction to Python Interfaces
  • Description of Python Extension
  • Elaborate on Python PyString Object
  • How to perform Python string operations?

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.

In PyThread_allocate_lock, a key struct PNRMUTEX is displayed. We find that this struct is the return value of the function, which is actually the interperter_lockGIL to be created in PyEval_InitThread ). It turns out GIL is this guy. Let's take a look at it.

 
 
  1. [thread_nt.h]  
  2.  
  3. PNRMUTEX AllocNonRecursiveMutex(void)  
  4.  
  5. {  
  6.  
  7.     PNRMUTEX mutex = (PNRMUTEX)malloc(sizeof(NRMUTEX)) ;  
  8.  
  9.     if(mutex && !InitializeNonRecursiveMutex(mutex)) {  
  10.  
  11.             free(mutex);  
  12.  
  13.             Mutex = NULL;  
  14.  
  15.     }  
  16.  
  17.     return mutex ;  
  18.  
  19. }  
  20.  
  21. BOOL InitializeNonRecursiveMutex(PNRMUTEX mutex)  
  22.  
  23. {  
  24.  
  25.     ……  
  26.  
  27.     mutex->owned = -1 ;  /* No threads have entered NonRecursiveMutex */  
  28.  
  29.     mutex->thread_id = 0 ;  
  30.  
  31.     mutex->hevent = CreateEvent(NULL, FALSE, FALSE, NULL) ;  
  32.  
  33.     return mutex->hevent != NULL ;  /* TRUE if the mutex is created */  
  34.  
  35. }  

In NRMUTEX, the types of all data members are the type styles on the Win32 platform. Both owned and thread_id are common, while HANDLE hevent is worth noting, let's take a look at what AllocNon-RecursiveMutex has prepared for this hevent.

Related Article

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.