First, what is a thread
Thread: As the name implies, is an assembly line work process, a pipeline must belong to a workshop, a workshop work process is a process
Therefore, a process is simply used to centralize resources together (a process is just a resource unit, or a resource collection), and the thread is the executing unit on the CPU.
The concept of multi-threaded (that is, multiple control threads) is that there are multiple control threads in a process, and multiple control threads share the process's address space, which is equivalent to a workshop with multiple pipelines, sharing a workshop resource. (a process with multiple threads (sharing memory space within the same process))
For example, the Beijing Metro and the Shanghai Metro are different processes, while the Beijing metro line Line 13 is a thread, the Beijing Metro all the lines share all the resources of the Beijing Metro, such as all passengers can be pulled by all lines.
Attention:
1. So the process inside the real work is the thread (process inside the cable)
2. The process is only used to isolate resources from each other, and the thread is really responsible for the CPU to mobilize his
Second, the thread creation cost is small
Is the cost of creating a process much larger than a thread?
If our software is a factory, the factory has a number of lines, the pipeline work requires power supply, only one CPU (single core CPU)
A workshop is a process, a workshop at least one pipeline (a process at least one thread)
Creating a process is creating a workshop (application space, building at least one pipeline within that space)
and build a thread, just in a workshop built a pipeline, no need to apply for space, so the creation of a small cost
Three, the difference between the thread and the process
1. Creating a thread is less than the process overhead (open a process, there is space, and the thread in the process, there is no need to open a space)
2. Multithreading must be open in a process, sharing resources within the process
3. Thread start-up speed fast
4. Multiple threads under the same process share the resources of the process, while the memory space between multiple processes is isolated
n = 100
def work ():
Global n
n-=100
If the open process n is independent of each other, and the thread is a shared resource, it does not isolate the
Under WINS, the child process does not copy the parent process's
Under Linux, the child process completely copies the parent process's
5. Threads can communicate with threads within the process in which they are located
Iv. Why to use multi-threaded
Multithreading refers to the opening of multiple threads in a process, simply speaking: If multiple tasks share a single address space, then multiple threads must be opened within a process. The detailed lecture is divided into 4 points:
1. Multithreading share the address space of a process
2. Threads are more lightweight than processes, threads are easier to create revocable than processes, and in many operating systems, creating a line turndown creates a process 10-100 times faster, which is useful when a large number of threads require dynamic and rapid modification
3. If multiple threads are CPU-intensive, there is no performance gain, but if there is a lot of computation and a lot of I/O processing, having multiple threads allows these activities to overlap each other, which speeds up the execution of the program.
4. In a multi-CPU system, in order to maximize the use of multicore, you can open multiple threads, much less than the open process overhead. (This one does not apply to Python)
Python Full stack Development foundation "23rd" thread