How to Create a Python multi-threaded Environment

Source: Internet
Author: User

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.

 
 
  1. [Threadmodule. c]
  2.  
  3. Static PyObject * thread_PyThread_start_new_thread (PyObject * self, PyObject
  4.  
  5. * Fargs)
  6.  
  7. {
  8.  
  9. PyObject * func, * args ,*Keyw=NULL;
  10.  
  11. Struct bootstate * boot;
  12.  
  13. Long ident;
  14.  
  15. PyArg_UnpackTuple (fargs, "start_new_thread", 2, 3, & func, & args,
  16.  
  17. & Keyw );
  18.  
  19. // [1]: Create a bootstate Structure
  20.  
  21. Boot=PyMem_NEW(Struct bootstate, 1 );
  22.  
  23. Boot->Interp=PyThreadState_GET()->Interp;
  24.  
  25. Boot->FuncFunc= Func;
  26.  
  27. Boot->ArgsArgs= Args;
  28.  
  29. Boot->KeywKeyw= Keyw;
  30.  
  31. // [2]: Initialize the multi-threaded Environment
  32.  
  33. PyEval_InitThreads ();/* Start the interpreter's thread-awareness */
  34.  
  35. // [3]: Create a thread
  36.  
  37. Ident=PyThread_start_new_thread(T_bootstrap, (void *) boot );
  38.  
  39. Return PyInt_FromLong (ident );
  40.  
  41. }

[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.

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.