In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about the differences between processes, threads and collaborative programs in Python. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
I. concept
1. Process
A process is a running activity of a program with a certain independent function on a certain data set, and a process is an independent unit of resource allocation and scheduling in the system. Each process has its own independent memory space, and different processes communicate through inter-process communication. Because the process is relatively heavy and occupies independent memory, the switching overhead between context processes (stack, register, virtual memory, file handle, etc.) is relatively large, but relatively stable and secure.
2. Thread
A thread is an entity of a process and the basic unit of CPU scheduling and dispatching. It is a smaller basic unit that can run independently than a process. The thread itself basically does not own system resources, only a few resources that are essential to running (such as program counters, a set of registers and stacks), but it can share all the resources owned by the process with other threads belonging to the same process. Inter-thread communication mainly through shared memory, context switching is very fast, less resource overhead, but compared with the unstable process, it is easy to lose data.
3. Cooperative process
The cooperative program is a kind of lightweight thread in the user mode, and the scheduling of the cooperative program is completely controlled by the user. The co-program has its own register context and stack. When scheduling and switching, the register context and stack are saved somewhere else, and when it is switched back, the previously saved register context and stack are restored, while the direct operation stack has almost no kernel switching overhead, and the global variables can be accessed without locking, so the context switching is very fast.
Second, the difference:
1. Processes are compared with threads.
A thread is an execution unit within a process and a schedulable entity within a process. The difference between threads and processes:
1) address space: a thread is an execution unit within a process. There is at least one thread in the process. They share the address space of the process, and the process has its own independent address space.
2) Resource ownership: a process is a unit of resource allocation and ownership, and threads in the same process share the resources of the process.
3) Thread is the basic unit of processor scheduling, but the process is not
4) both of them can be executed concurrently
5) each independent thread has an entry, sequential execution sequence and exit of the program, but the thread cannot execute independently and must be stored in the application, and multiple thread execution control must be provided by the application.
2. More cooperative programs are compared with threads.
1) A thread can have multiple cooperators, or a process can have multiple collaborators alone, so multicore CPU can be used in python.
2) Thread processes are synchronous, while cooperative processes are asynchronous.
3) the co-program can retain the state of the last call, and each reentry of the procedure is equivalent to entering the state of the last call.
III. The use of processes, threads and co-programs in python
1. Multi-processes generally use multiprocessing libraries to take advantage of multicore CPU, mainly for CPU-intensive programs, but producers and consumers can also use this. The advantage of multi-process is that the collapse of one child process will not affect the operation of other child processes and main processes, but the disadvantage is that it can not start too many processes at one time, which will seriously affect the resource scheduling of the system, especially the CPU utilization and load. Using multiple processes, you can view the article "Summary of python Multi-process usage". Note: there is a problem with the use of python2's process pool in the class, and the class function needs to be defined as a global function. For more information, please refer to http://bbs.chinaunix.net/thread-4111379-1-1.html
2. Multithreading generally uses threading library to complete some IO-intensive concurrent operations. The advantage of multithreading is fast switching and low resource consumption, but a thread hanging will affect all threads, so it is not stable enough. There are many scenarios in which thread pools are used in reality. For more information, please see "python thread pool implementation".
3, the cooperation program generally uses the gevent library, of course, this library is more troublesome to use, so it is not used very much. On the contrary, co-program is much more used in tornado. Using co-program to make tornado single-threaded asynchronous is said to solve the problem of C10K. Therefore, the most commonly used place for co-programming is in web applications.
To sum up, IO-intensive generally uses multi-threads or multi-processes, CPU-intensive generally uses multi-processes, emphasizing that non-blocking asynchronous concurrency generally uses cooperative programs, of course, sometimes it requires the combination of multi-process thread pools, or other combinations.
After reading the above, do you have any further understanding of the differences between processes, threads, and collaborators in Python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.