Briefly describe the differences and application scenarios of processes, threads, and coroutines?

Briefly describe the differences and application scenarios of processes, threads, and coroutines?

1. Concept:

1. Process

  • Process: A running program or code is a process, and a code that is not running is called a program. The process is the smallest unit for resource allocation in the system. The process has its own memory space, so data is not shared between processes and the overhead is high.
  • A process is a program with certain independent functions that performs a running activity on a certain data set. A process is an independent unit for resource allocation and scheduling in the system. Each process has its own independent memory space, and different processes communicate through inter-process communication. Since processes are relatively heavy and occupy independent memory, the switching overhead (stack, register, virtual memory, file handle, etc.) between context processes is relatively large, but it is relatively stable and safe.
  • The process has its own independent heap and stack, the heap and stack are not shared, and the process is scheduled by the operating system

2. Thread

  • Thread: The smallest unit of scheduling execution, also called the execution path, cannot exist independently, but depends on the existence of the process. A process has at least one thread, called the main thread, and multiple threads share memory (data sharing and global variables), so it is improved Program efficiency.
  • A thread is an entity of a process and is the basic unit of CPU scheduling and dispatch. It is a basic unit that is smaller than a process and can run independently. The thread itself basically does not own system resources, only a few resources that are essential during operation. (such as a program counter, a set of registers and a stack), but it can share all the resources owned by the process with other threads belonging to the same process. Inter-thread communication is mainly through shared memory, context switching is fast, and resource overhead is less, but compared with processes that are not stable enough and can easily lose data.
  • Threads have their own independent stacks, but the heap is shared. Standard threads are scheduled by the operating system.

3. Coroutine

  • Coroutine: A lightweight thread in user mode. The scheduling is controlled by the user. It has its own register context and stack. There is basically no kernel switching overhead when switching, and the switching is flexible.
  • Coroutine is a lightweight thread in user mode, and the scheduling of coroutine is completely controlled by the user. Coroutines have their own register context and stack. When the coroutine schedule is switched, the register context and stack are saved to other places. When switching back, the previously saved register context and stack are restored. There is basically no kernel switching overhead for direct operation of the stack, and global variables can be accessed without locking. , so context switching is very fast.
  • The coroutines share the heap but not the stack. The coroutines are scheduled by the programmer in the code block of the coroutine

2. Difference:

1. Comparing multiple processes with threads

A thread refers to an execution unit within a process and is also a schedulable entity within the process. The difference between threads and processes:

  1. Address space: A thread is an execution unit in a process. There is at least one thread in a process. They share the address space of the process, and the process has its own independent address space.
  2. Resource ownership: The process is the unit of resource allocation and ownership. Threads in the same process share the resources of the process.
  3. Threads are the basic unit of processor scheduling, but processes are the basic unit of resource allocation.
  4. Both can be executed concurrently
  5. Each independent thread has an entry point for program running, a sequential execution sequence, and a program exit. However, threads cannot execute independently and must be dependent on the application program. The application program provides multiple thread execution control.

2. Compare coroutines with threads

  1. A thread can have multiple coroutines, and a process can also have multiple coroutines independently, so that multi-core CPUs can be used in python.
  2. Thread processes are all synchronous mechanisms, while coroutines are asynchronous
  3. The coroutine can retain the state of the last call. Each time the process re-enters, it is equivalent to entering the state of the last call.

3. The use of processes, threads, and coroutines in python

1. Multiprocessing generally uses the multiprocessing library to take advantage of multi-core CPUs. It is mainly used in CPU-intensive programs. Of course, producers and consumers can also be used. The advantage of multi-process is that the crash of one sub-process will not affect the operation of other sub-processes and the main process. However, the disadvantage is that too many processes cannot be started at one time, which will seriously affect the resource scheduling of the system, especially the CPU usage and load.

2. Multi-threading generally uses the threading library to complete some IO-intensive concurrent operations. The advantages of multi-threading are fast switching and low resource consumption, but if one thread hangs up, all threads will be affected, so it is not stable enough.

3. Coroutines generally use the gevent library. Of course, this library is more troublesome to use, so it is not used much. On the contrary, coroutines are used much more in tornado. Using coroutines allows tornado to be single-threaded and asynchronous. It is said that it can also solve the C10K problem. Therefore, coroutines are most commonly used in web applications.

Note: IO-intensive types generally use multi-threads or multi-processes, CPU-intensive types generally use multi-processes, and those that emphasize non-blocking asynchronous concurrency generally use coroutines. Of course, sometimes it is also necessary to combine multi-process thread pools, or other combinations Way.

Guess you like

Origin blog.csdn.net/weixin_53909748/article/details/132202035