Multithreading and multiprocessing of python concurrent programming

 

1 What is a thread

In the operating system, each process has an address space, and by default there is a thread of control

The concept of multithreading (that is, multiple control threads) is that there are multiple control threads in a process, and multiple control threads share the address space of the process, which is equivalent to multiple pipelines in a workshop, all sharing the resources of a workshop .

 

The cost of creating two threads is small

Is the overhead of creating a process much greater than that of a thread?

If our software is a factory, the factory has multiple pipelines, the pipeline work requires power, and there is only one power supply, that is, the cpu (single-core cpu)

A workshop is a process, and a workshop has at least one pipeline (a process has at least one thread)

To create a process is to create a workshop (apply for a space and build at least one assembly line in this space)

To build a thread is to build an assembly line in a workshop without applying for space, so the cost of creation is small.

 

Is there a competitive relationship between processes and a cooperative relationship between threads?

The workshop is directly related to competition/power grabbing, competition (different processes are directly competitive, programs written by different programmers are run, Xunlei preempts the network speed of other processes, and 360 kills other processes as viruses)
A ​​workshop The relationship between different pipelined collaborative work (the threads of the same process are cooperative relationships, and are started within the program written by the same program, and the threads in Thunder are cooperative relationships, and they will not do their own work)

Four why use multithreading

  Multithreading refers to opening multiple threads in a process. Simply put: if multiple tasks share an address space, multiple threads must be opened in a process. The detailed talk is divided into 4 points:

  1. Multiple threads share the address space of a process

      2. Threads are more lightweight than processes, and threads are easier to create and undo than processes. In many operating systems, creating a thread is 10-100 times faster than creating a process. When there are a large number of threads that need to be dynamically and quickly modified, This feature is very useful

      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 run on top of each other, thus speeding up The speed of program execution.

      4. In a multi-cpu system, in order to maximize the use of multi-core, multiple threads can be opened, which is much less expensive than opening a process. (this one does not apply to python)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324813259&siteId=291194637