Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement the execution Framework of Python Virtual Machine

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to implement the implementation framework of the Python virtual machine, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

In the Python virtual machine, a core concept, in Python language, everything is an object, that is, an integer is an object, a string is also an object, in fact, the type is also an object, the integer type is an object.

Understanding these two points is sufficient to master the content of the second part. However, the virtual machine and execution environment are only part of the running mechanism of Python (or running model). In order to have a comprehensive understanding of the whole running mechanism of Python, we also need to have a general understanding of the runtime environment of Python.

As we said earlier, PyFrameObject corresponds to the stack frame when the executable is executed, but the stack frame is not enough for an executable to run in the operating system. Previously we left out two concepts that are critical to running executable files: processes and threads.

First of all, we need to have an overall conceptual understanding of the running model of Python (mainly the threading model), although we will leave this part to analyze the multithreaded implementation of Python in detail. However, because the Python virtual machine creates a main thread during initialization.

Therefore, there is a main thread in its runtime environment, and the exception mechanism of Python that will be analyzed in this section will make use of the thread model within Python, so it is necessary to have an overall conceptual understanding of the Python thread model. Taking the Win32 platform as an example, we know that native Win32 executables, whether generated by CUnip Cobb + or by Delphi, will run in a Process.

The process is not the active object corresponding to the machine instruction sequence, this active object corresponding to the machine instruction sequence in the executable file is abstracted by the concept of Thread, and the process is the active environment of the thread. For the usual single-threaded executable, the operating system creates a process at execution time, and there is another main thread in the process, while for multithreaded executable files, the operating system creates one process and multiple threads during execution.

The multiple threads can share global variables in the process address space, which naturally leads to the problem of thread synchronization. The task switching of CPU is actually switching between threads. When switching tasks, CPU needs to save the thread environment, and after switching to a new thread, it needs to restore the thread environment of that thread.

These concepts about program running also apply to Python,Python 's support for multithreading, and a thread in Python is a native thread on the operating system. We don't go too far into the multithreading mechanism here, and now we just need to keep in mind that there may be multiple threads when Python executes.

Earlier, we saw the general running framework of the virtual machine. In fact, this virtual machine is the abstraction of CPU in Python, which can be seen as a soft CPU,Python in which all threads use this soft CPU to complete the computing work. The task switching mechanism on the real machine corresponds to Python.

This is the mechanism by which different threads take turns using virtual machines. CPU needs to save the thread running environment when switching tasks. For Python, you also need to save information about the current thread before switching threads. In Python, this abstraction of thread state information is achieved through a PyThreadState object, and a thread will have a PyThreadState object.

So in another sense, this PyThreadState object can also be seen as an abstraction of the thread itself. But in fact, there is a big difference between the two. PyThreadState is not a simulation of the thread itself, because threads in Python still use the operating system's native threads.

PyThreadState is just an abstraction of thread state, but in most chapters of this book, we don't distinguish between threads and thread state itself too strictly for the sake of description. So in the future, we sometimes call PyThreadState a thread object, and sometimes we call it a thread state object. It is only when we analyze the multithreading mechanism that we make a strict distinction between the two. There are similar considerations for the PyInterpreterState object that will be mentioned below.

As mentioned just now, under Win32, a thread cannot survive independently, it needs to live in the environment of a process, and multiple threads can share some of the resources of the process. The same is true in Python. Consider that if there are two threads in a Python program that both do the same action, import sys, how many copies of this sys module should exist?

Is it globally shared or does each thread have a sys module? If each thread had its own independent module collection, the memory consumption of Python would be staggering. So in Python, these module are shared globally, as if these module are shared resources in a process. For the abstract concept of process, Python is implemented as a PyInterpreterState object.

Under Win32, there are usually multiple processes, while Python can actually have multiple logical interpreter. Under normal circumstances, there is only one interpreter in the Python. One or more PyThreadState objects are maintained in this interpreter, and the threads corresponding to these PyThreadState objects take turns using a bytecode execution engine. Look, is it very similar to the program execution model on a real machine?

When it comes to multithreading, we have to talk about thread synchronization. In the Python virtual machine, thread synchronization is achieved through a global interpreter lock GIL (Global Interpreter Lock), which we will examine in detail when we analyze the Python multithreading mechanism.

Okay, now let's talk about the two key objects just mentioned: the PyInterpreterObject object that represents the process concept and the PyThreadState object that represents the thread concept.

After reading the above, do you have any further understanding of how to implement the implementation framework of Python virtual machine? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report