This article is participating in Python Theme Month. See the link for details

On wechat, Rest cannot be enjoyed by lazy people.

Previously: Zhuang’s interview was full of twists and turns. The interviewer invited me to interview for multi-threading and multi-process. Let’s see how Zhuang dealt with it wisely

Interviewer: Young man, let’s talk about the process thread. Let’s talk about the process first.

Me: Code stored on hard disk is static file, running program is called process. Data is isolated between processes.

Generally speaking, a process does not run continuously from beginning to end, and it interacts with the execution of other processes in concurrent execution. There are at least three basic states during the activity of a process: running state, ready state, and blocked state.

① After a process is created and initialized, it becomes ready.

(2) After the process in the ready state is selected by the process scheduler of the operating system, it is assigned to the CPU to officially run the process and enter the running state;

(3) When a process is running or an error occurs, it will be processed by the operating system as the end state.

(4) During the running process, the operating system will change the running process to ready state because the running time slice allocated to it is used up, and then select another process from ready state to run;

⑤ When a process requests an event and must wait, such as requesting an I/O event, it will change from a running state to a blocked state;

⑥ The process changes from a blocked state to a ready state when the event it is waiting for completes

Interviewer :(thinking, he’s got it all) what about threads?

Me: in the process of the thread is a line, is the unit of the CPU scheduling, a process can have multiple threads at the same time, the same process in the threads share all the resources in the current process, if for example, is the process of a workshop, the thread is equivalent to the workshop of the assembly line, assembly line needed raw materials need to be obtained from the workshop. So a process is a unit of resources and a thread is a unit of execution, and creating a thread is more resource-efficient than creating a process.

Interviewer: Do you know anything about coroutines in Python

I: In Python, coroutine refers to the realization of concurrency under a single thread. The developer detects IO operations at the code level, and once the program encounters IO operations, the switch will be completed at the code level. In this way, the CPU feels that the program is always running without IO operations, thus improving the running efficiency of the program. Coroutines can be implemented in Python with the help of the GEvent module. The GEvent module itself cannot detect some common IO operations and requires monkey patches.

Interviewer: Do you know the GIL lock in Python?

Me: The GIL is python’s global interpreter lock, and the GIL is a CPython feature that prevents multiple threads from executing simultaneously in the same process. The GIL exists because CPython’s memory management is not thread-safe.

Execution code need to be explained to the interpreter, because all the threads are in the process of sharing resources within the process, it is the competition, and garbage collection mechanism is also a threads in the current process, the thread and other threads competing for the current process data, to ensure data security, the process only one thread running in the same time have the GIL.

Interviewer: Yes, the basic knowledge of process and thread is not bad, let’s make an appointment to talk about the network

Me :(it’s strange to have this interviewer next time) ok, take your time.