Multi-threading and multi-process-brief

Multi-process and multi-thread

Multithreading can achieve code parallelism and improve operating efficiency. Python uses threading and Queue modules to achieve multi-threaded programming.

Thread and process distinction

process

The heavyweight process is a program execution. Each process has its own memory space and data stack, and can only use interprocess communication (IPC), not directly share information.

The size of the process pool is the number of processes that are executed at the same time, but it does not affect the number of processes applied for by the main process. The amount of multi-process applied by the main process is not equal to the pool size [3] .

Multi-process blocking and non-blocking [2]

Non-blocking : no need to wait. Start several processes and run several processes at the same time.

  • Advantages: fast running speed;
  • Disadvantages: The output may be messy (to be verified). But the child process must execute the parent process only after the operation is completed.

Blocking : You need to wait for process 1 to finish before running process 2. Even if multiple processes are started at the same time, it is necessary to wait for one process to end before running the next.

  • Advantages: The output sequence is reasonable, which is equivalent to adding a lock and releasing a lock (to be verified). You can use blocking if you have requirements on the output;
  • Disadvantages: The running speed is relatively slow, and you need to wait for one process to finish before running the next process.

Thread

Lightweight processes are similar to processes, except that all threads run in the same process and share the same operating environment. The thread consists of three parts: start, sequential execution and end. Multiple threads in a process share the same data space, so threads can more easily share data and communicate with each other than processes.

reference

[1] https://www.cnblogs.com/hanfei-1005/category/854157.html

[2] https://blog.csdn.net/qq_41562377/article/details/105219269

[3] https://blog.csdn.net/u012969412/article/details/82768882

unread

[1] https://thief.one/2016/11/23/Python-multiprocessing/

Guess you like

Origin www.cnblogs.com/yefan19/p/12728162.html