Instructions on executing the Python Interpreter

Source: Internet
Author: User

The Python interpreter is a system software that can execute programs written in other computer languages. It is a translation program. In contrast, the source code interpreter is easier to create, there is no need for an independent compilation process, which greatly increases developers' interest.

In this step, the sub-thread suspends itself, and the thread scheduling mechanism of the operating system can no longer wake up by its own strength, only after the Python thread scheduling mechanism forces the main thread to give up GIL. The sub-thread will be awakened. After the sub-thread is awakened, the main thread is stuck in waiting, and it is also struggling to watch the moment that Python forces the sub-thread to give up GIL.

After a subthread is awakened by the Python thread scheduling mechanism, the first thing it does is to set the current thread state object maintained by Python to its own State object through PyThreadState_Swap, it is the same as restoring the Context Environment of the operating system process.

Now our sub-thread is waiting for GIL, but note that the initialization of the thread is not completed yet, because the sub-thread has not smoothly entered the bytecode interpreter. After the Python thread is scheduled to wake up the Sub-thread. The sub-thread will return to t_bootstrap and go to PyEval_CallObjectWithKeywords. From here, we will call PyEval_EvalFrameEx to enter the Python interpreter. At that time, the child thread is completely controlled by the Python thread scheduling mechanism, just like the main thread.

It should be noted that PyThread_start_new_thread is executed in the main thread, and from bootstrap, it is executed in the Child thread. The action involving thread destruction, such as PyThreadState _ DeleteCurrent, will be analyzed in the subsequent sections.

At this point, readers may have some doubts. It seems useless to spend a lot of time analyzing the linked list of thread state objects. Otherwise, just think about it.


When Thread Scheduling occurs, at the Python level, you need to switch the current thread state object through the PyTrheadState_Swap function that has been analyzed previously, in this case, you need to obtain the thread object from the linked list of the thread state object based on the thread id.

In fact, many APIs in Python, such as PyGILState_Ensure, involve this linked list. These APIs may be called in large quantities during interaction between C and Python, interested readers can explore it in depth.

The Python interpreter consists of two major subsystems: an expression parser, which processes numeric expressions, and an interpreter, which is responsible for the actual execution of programs. For the former, you can use the expression parser described in Chapter 2nd of this book. However, some improvements have been made here, so that the parser can parse the numeric expressions contained in program statements, rather than just parsing isolated expressions.

The interpreter subsystem and parser subsystem are included in the same interpreter class, which is called SBasic. Although theoretically two independent classes can be used: one containing the Python interpreter and the other containing the expression parser, the generation efficiency of the two using the same class is higher.

Because the expression parser and the Python interpreter code are inseparable. For example, both subsystems operate to save the same character array of program code. If they are arranged in two classes, they will increase the additional overhead and lead to performance loss and functional duplication. In addition, due to the heavy workload of program interpretation and the parsing expression is only part of it, it makes sense to include the entire interpretation mechanism in a single class.

Each time the Python interpreter is executed, an identifier is read from the source code of the program. If a keyword is read, the interpreter performs the specified operation according to the requirements of the keyword. For example, when the interpreter reads a PRINT, it prints the characters after the PRINT; when it reads a GOSUB, it executes the specified subroutine. This process will be repeated until the end of the program is reached. As you can see, the interpreter simply executes the action specified by the program.

  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.