Python Program Execution Process
1. Is Python an interpreted language?
When I was a beginner in Python, the first sentence I heard about Python was that Python was an explanatory language, so I kept believing until I found the existence of the *. pyc file. If it is an interpreted language, what is the *. pyc file generated? C should be the abbreviation of compiled!
To prevent other Python learners from being misunderstood, let's clarify this question and clarify some basic concepts.
2. interpreted and compiled languages
Computers cannot recognize advanced languages, so when we run an advanced language program, A "translator" is needed to convert advanced languages into machine languages that computers can understand. This process is divided into two types: Compilation and interpretation.
Before executing a program, the compiled language will first execute a compilation process on the program through the compiler to convert the program into a machine language. You do not need to translate it during the running, but simply execute it. The most typical example is the C language.
This compilation process does not exist in the interpreted language. Instead, the interpreter is used to explain the program line by line and then run it directly. The most typical example is Ruby.
Through the above example, we can summarize the advantages and disadvantages of interpreted and compiled languages, because the compiled language has already translated the program before it runs the program ", therefore, the process of "Translation" is reduced during operation, so the efficiency is relatively high. However, we cannot generalize either. Some interpreted languages can also optimize the entire program during Program Translation through interpreter optimization, thus surpassing the compiled language in efficiency.
In addition, with the rise of virtual machine-based languages such as Java, we cannot simply divide the language into two types: interpreted and compiled.
Java is used as an example. Java is first compiled into a bytecode file through the compiler, and then interpreted as a machine file through the interpreter at runtime. Therefore, Java is a language that is compiled first and then interpreted.
Change to C #, C # first, compile the C # file into the IL file through the compiler, and then compile the IL file into the machine file through CLR. Therefore, C # is a pure compilation language, but C # is a language that requires secondary compilation. Similarly, it can be equivalent to other languages on the. NET platform.
3. What is Python?
In fact, Python is also a virtual machine-based language like Java/C #. Let's take a look at the running process of the Python program.
When we input python hello. py in the command line, it actually activates the "interpreter" of Python and tells the "interpreter" that you are about to start working. However, before "explanation", the first task to be executed is compiling Like Java.
If you are familiar with Java, you can think about how to execute a Java program in the command line:
Javac hello. java
Java hello
It's just that when we use an IDE such as Eclipse, we integrate these two parts into one. The same is true for Python. When we execute python hello. py, he also executes this process, so we should describe Python in this way, Python is a language that is compiled first and then interpreted.
4. Briefly describe the Python running process
Before talking about this, let's talk about two concepts: PyCodeObject and pyc files.
The pyc we see on the hard disk naturally does not need to say much, but in fact, PyCodeObject is actually compiled by the Python compiler. We can simply know it and continue to look down.
When the python program runs, the compilation result is saved in the PyCodeObject in the memory. When the Python program runs, the Python interpreter writes the PyCodeObject back to the pyc file.
When the python program runs for the second time, the program first looks for the pyc file in the hard disk. if it finds the file, it loads it directly; otherwise, the above process will be repeated.
Therefore, we should locate the PyCodeObject and pyc files in this way. We say that the pyc file is actually a persistent storage method of PyCodeObject.
5. Run a Python Program
Let's write a program to run it:
The program itself is meaningless. Let's continue:
However, we didn't see the pyc file in the program, and still test. py stayed there alone!
In another way, we change the print_str method to another python module:
Then run the program:
At this time, the pyc File appeared. In fact, it is not difficult to think about the cause. Let's take a look at the actual business situation.
6. The purpose of pyc is to reuse
When I think about the advantages and disadvantages of the second section in this article, the advantage of the compilation language is that we don't need to explain it when running the program, directly use the files that have been "translated. That is to say, the biggest advantage of compiling the py file into the pyc file is that we do not need to re-interpret the module when running the program.
Therefore, what we need to compile into a pyc file should be those Reusable Modules, which is the same purpose in designing software classes. Therefore, the Python interpreter believes that only the imported modules need to be reused.
At this time, someone may say, no! Your problem has not been explained. Does my test. py need to be run? Although it is not a module, it can save time every time I run it!
Okay. Let's start from the actual situation and think about when we can run the python xxx. py file:
A. during the test.
B. Start a Web process.
C. execute a program script.
Let's talk about the first case one by one. In this case, even if no pyc file exists in all files, it doesn't matter.
In the second case, let's imagine a webpy program. We usually execute it like this:
Suppress or:
Then this program monitors port 8181/9002 like a daemon, And once interrupted, it may only be the program killed, or other unexpected situations, you need to restart the entire Web service. Since we have been monitoring, it is enough to keep PyCodeObject in the memory, and there is no need to persist it on the hard disk.
In the last case, execute a program script. The main entry of a program is similar to the Controller in the Web program. That is to say, it is responsible for scheduling between models, it does not contain any main logic, such.
So we can understand the intention of the Python interpreter in this way. The Python interpreter only permanently converts the modules we may reuse into the pyc file.
7. expiration time of pyc
After finishing the pyc file, someone may think that every time the Python interpreter persistently converts the module into a pyc file, when my module changes, are you sure you want to manually remove the previous pyc file?
Of course, Python designers will not make such an idiotic mistake. This process depends on how PyCodeObject is written into the pyc file.
Let's take a look at the source code of the import process:
This code is Long. Let's just look at the code I labeled. In fact, when he writes a pyc file, he writes a Long variable, the variable content is the last modification date of the file. Similarly, let's look at the code for loading pyc:
Without looking at the code carefully, we can clearly see the principle. In fact, every time before loading, we will first check the last modification date saved in the py and pyc files, if they are inconsistent, generate a new pyc file.
8. written at the end
In fact, understanding the execution process of Python programs is of little significance to most programmers, including Python programmers. What makes sense is what we can learn from the practice of the Python interpreter, I think there are several points like this:
A. in fact, whether Python is saved as a pyc file is the same as when designing a cache system. We can think about what is worth throwing in the cache, nothing is worth throwing in the cache.
B. When running a time-consuming Python script, how can we slightly squeeze the running time of some programs is to separate the modules from the main modules. (Although this is often not a bottleneck)
C. Should reusable and non-reusable items be considered separately when designing a software system? This is an important part of the software design principles.
D. How can we avoid program expiration when designing a cache system (or other systems)? In fact, the Python interpreter also provides us with a particularly common and effective solution.