How to establish a main Python thread?

Source: Internet
Author: User

In Python, If you create a program, it is a process, including a thread, which is the main thread, but to improve the resource usage efficiency and system efficiency, I hope you can learn the information you want again.

From this we can see that when a thread starts to wait for GIL, its owned will be increased by 1. Obviously, we can guess that when a thread finally releases GIL, it will definitely subtract 1 from the GIL owned so that after all the threads that need GIL are finally released, owned will change to-1 again, meaning GIL will become available again.

To clearly display this point, let's take a look at the inverse operation of PyThread_aquire_lock, pyThread_release_lock every thread that changes from running to waiting state will call it before being suspended to release the possession of GIL.

 
 
  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 the end, when a thread releases GIL, it will notify all the threads waiting for the hevent of GIL through SetEvent. Combined with the previous analysis, if there is a thread waiting for the hevent of GIL at this time, then it will be awakened by the operating system.

This is the mechanism that we introduced earlier in Python to delegate the second difficulty of thread scheduling to the operating system. At this time, the thread that calls PyEval_InitThread, that is, the main Python thread, has successfully obtained GIL. Finally, PyThread_get_thread_ident () is called ().

Use the Win32 API: GetCurrent-ThreadId to obtain the id of the current main Python thread and assign it to main_thread. main_thread is a static global variable that stores the thread id of the main Python thread, used to determine whether a thread is a main Python thread.

After the multi-threaded environment is initialized, Python will start to create the native thread of the underlying platform. Taking thread1.py as an example, this native thread will execute the operations defined by threadProc. From now on, to clarify the description, we call the main Python thread, namely thread_PyThread_start_new_thread, to create a new thread named as the main thread, the native thread corresponding to threadProc is called a subthread. Now let's take a look at how a subthread is created.

 
 
  1. Static PyObject * thread_PyThread_start_new_thread (PyObject * self, PyObject
  2.  
  3. * Fargs)
  4.  
  5. {
  6.  
  7. PyObject * func, * args ,*Keyw=NULL;
  8.  
  9. Struct bootstate * boot;
  10.  
  11. Long ident;
  12.  
  13. PyArg_UnpackTuple (fargs, "start_new_thread", 2, 3, & func, & args, & keyw );
  14.  
  15. // [1]: Create a bootstate Structure
  16.  
  17. Boot=PyMem_NEW(Struct bootstate, 1 );
  18.  
  19. Boot->Interp=PyThreadState_GET()->Interp;
  20.  
  21. Boot->FuncFunc= Func;
  22.  
  23. Boot->ArgsArgs= Args;
  24.  
  25. Boot->KeywKeyw= Keyw;
  26.  
  27. // [2]: Initialize the multi-threaded Environment
  28.  
  29. PyEval_InitThreads ();/* Start the interpreter's thread-awareness */
  30.  
  31. // [3]: Create a thread
  32.  
  33. Ident=PyThread_start_new_thread(T_bootstrap, (void *) boot );
  34.  
  35. Return PyInt_FromLong (ident );
  36.  
  37. [Thread. c]
  38.  
  39. /* Support for runtime thread stack size tuning.
  40.  
  41. A value of 0 means using the platform's default stack size
  42.  
  43. Or the size specified by the THREAD_STACK_SIZE macro .*/
  44.  
  45. Static size_t_ Pythread_stacksize=0;
  46.  
  47. [Thread_nt.h]
  48.  
  49. Long PyThread_start_new_thread (void (* func) (void *), void * arg)
  50.  
  51. {

The main Python thread calls PyThread_start_new_thread to create a subthread. To clearly understand the work of PyThread_start_new_thread, we need to pay special attention to the parameters of this function. From thread _ PyThread_start_new_thread, we can see that func is actually the t_bootstrap function, while arg is the bootstate structure boot created in thread_PyThread_start_new_thread. In boot, stores the thread information defined in the Python program thread1.py.

  1. How to embed Python into C ++ applications?
  2. In-depth discussion of Ruby and Python syntax comparison
  3. Introduction to Python
  4. Python Learning Experience: version, IDE selection and encoding solution
  5. Analysis of Python GIL and thread security

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.