1+ # ########################################Process vs Thread################################
2+
3+ # We introduced multiprocessing and multithreading, two of the most common ways to implement multitasking.
4+ # Now, let's discuss the pros and cons of both approaches.
5+
6+ # First of all, to achieve multi-tasking, we usually design the Master-Worker mode.
7+ # The Master is responsible for assigning tasks, and the Worker is responsible for executing tasks.
8+ # Therefore, in a multi-tasking environment, there is usually one Master and multiple Workers. (※)
9+
10+ # If the Master-Worker is implemented with multiple processes(※), the main process is the Master,
11+ # and the other processes are the Workers.
12+
13+ # If the Master-Worker is implemented with multiple threads(※), the main thread is the Master,
14+ # and the other threads are the Workers.
15+
16+ # The biggest advantage of multi-process mode is high stability,
17+ # because a child process crashes, it will not affect the main process and other child processes.
18+ # (Of course, the main process hangs up all the processes, but the Master process is only responsible for
19+ # allocating tasks, and the probability of hanging up is low.)
20+ # The famous Apache first adopted the multi-process mode.(※)
21+
22+ # The disadvantage of the multi-process mode is that the cost of creating a process is high.
23+ # In Unix/Linux systems, it fork is OK to use calls, and the cost of creating processes in Windows is huge.
24+ # In addition, the number of processes that the operating system can run at the same time is also limited.
25+ # Under the constraints of memory and CPU, if there are thousands of processes running at the same time,
26+ # the operating system will even have scheduling problems.
27+
28+ # Multi-threaded mode is usually a little faster than multi-process, but not much faster,
29+ # and the fatal disadvantage of multi-threaded mode is that any thread hangs may directly cause
30+ # the entire process to crash, because all threads share the memory of the process.
31+ # On Windows, if there is a problem with the code executed by a thread, you can often see this prompt:
32+ # "The program has performed an illegal operation and is about to close."
33+ # In fact, there is often a problem with a thread, but the operating system will force End the entire process.
34+
35+ # # Under Windows, multi-threading is more efficient than multi-process, so Microsoft's IIS server adopts
36+ # multi-threading mode by default. Due to the stability problem of multi-threading,
37+ # the stability of IIS is not as good as that of Apache. In order to alleviate this problem, IIS and Apache now
38+ # have a mixed mode of multi-process + multi-threading, which really complicates the problem.
39+
40+ # ########################################Thread Switching################################
41+
42+ # Whether it is multi-process or multi-threaded,
43+ # as long as the number is large, the efficiency will definitely not go up, why?
44+
45+ # Let's take an analogy.
46+ # Suppose you are unfortunately preparing for the senior high school entrance examination.
47+ # You need to do homework in 5 subjects of Chinese, mathematics, English, physics, and chemistry every night.
48+ # Each homework takes 1 hour.
49+ # If you spend 1 hour doing the language homework first, and then spend 1 hour doing the math homework,
50+ # and then do it all in turn, it will take a total of 5 hours. This method is called a single-task model,
51+ # or a batch task model.
52+
53+ # Suppose you plan to switch to a multitasking model, you can do Chinese for 1 minute,
54+ # then switch to math homework, do 1 minute, then switch to English, and so on, as long as the switching
55+ # speed is fast enough, this method will be executed with a single-core CPU. Multitasking is the same.
56+ # From the point of view of a kindergartener, you are doing 5 homework at the same time.
57+
58+ # However, switching homework comes at a cost. For example, when switching from Chinese to mathematics,
59+ # you must first clean up the Chinese books and pens on the desk (this is called saving the scene),
60+ # then, open the mathematics textbook and find a compass ruler (this is called preparing for a new environment) )
61+ # to start doing math homework. The same is true when the operating system switches processes or threads.
62+ # It needs to save the currently executed on-site environment (CPU register state, memory page, etc.),
63+ # and then prepare the execution environment of the new task (restore the last register state, switch memory
64+ # pages, etc.) to start execution. Although this switching process is fast, it also takes time.
65+ # If there are thousands of tasks running at the same time, the operating system may be mainly busy switching
66+ # tasks, and there is not much time to perform tasks.
67+
68+ # Therefore, once the multitasking reaches a limit, it will consume all the resources of the system, resulting in
69+ # a sharp drop in efficiency, and all tasks cannot be done well.
70+
71+
72+ # ##################################Compute-intensive vs. IO-intensive#######################
73+
74+ # A second consideration for multitasking is the type of task.
75+ # We can divide tasks into compute-intensive and IO-intensive.
76+
77+ # Computation-intensive tasks are characterized by a large amount of computation and CPU resource consumption,
78+ # such as calculating the pi ratio, decoding video in high-definition, etc., all of which depend on the computing
79+ # power of the CPU. Although this kind of computing-intensive task can also be completed by multitasking,
80+ # the more tasks, the more time spent in task switching, and the lower the efficiency of the CPU to perform tasks.
81+ # The number of simultaneous tasks should be equal to the number of CPU cores.
82+
83+ # Computation-intensive tasks mainly consume CPU resources, so the efficiency of the code is very important.
84+ # Scripting languages like Python are inefficient and completely unsuitable for computationally intensive tasks.
85+ # For computationally intensive tasks, it is best to write in C.
86+
87+ # The second type of task is IO-intensive. Tasks involving network and disk IO are all IO-intensive tasks.
88+ # This type of task is characterized by low CPU consumption and most of the time of the task is waiting for the
89+ # IO operation to complete (because The speed of IO is much lower than the speed of CPU and memory).
90+ # For IO-intensive tasks, the more tasks, the higher the CPU efficiency, but there is a limit. Most common tasks
91+ # are IO-intensive tasks, such as web applications.
92+
93+ # During the execution of IO-intensive tasks, 99% of the time is spent on IO, and very little time is spent
94+ # on the CPU. Therefore, it is completely impossible to replace the extremely slow scripting language such as
95+ # Python with the extremely fast C language. Improve operational efficiency. For IO-intensive tasks, the most
96+ # suitable language is the language with the highest development efficiency (the least amount of code),
97+ # the scripting language is the first choice, and the C language is the worst.
98+
99+
100+ # ##################################Asynchronous IO#######################
101+ # Considering the huge speed difference between CPU and IO, a task spends most of the time waiting for IO
102+ # operations during execution.
103+ # The single-process single-threaded model will prevent other tasks from being executed in parallel.
104+ # Therefore, we need a multi-process model. Or a multithreading model to support concurrent execution of
105+ # multiple tasks.
106+
107+ # Modern operating systems have made huge improvements to IO operations, and the biggest feature is
108+ # support for asynchronous IO.
109+ # If you make full use of the asynchronous IO support provided by the operating system, you can use
110+ # a single-process single-thread model to perform multitasking. This new model is called an event-driven model.
111+ # Nginx is a web server that supports asynchronous IO. It runs on a single-core CPU. The single-process model
112+ # can efficiently support multitasking. On a multi-core CPU, you can run multiple processes (the same number
113+ # as the number of CPU cores), taking full advantage of the multi-core CPU. Because the total number of processes
114+ # in the system is very limited, the operating system scheduling is very efficient. Multitasking with the asynchronous
115+ # IO programming model is a major trend.
116+ # Corresponding to the Python language, the single-threaded asynchronous programming model is called coroutines.
117+ # With the support of coroutines, efficient multitasking programs can be written based on event driving.
118+ # We'll discuss how to write coroutines later.
0 commit comments