


The differences and advantages and disadvantages of threads and processes in python
In this article, let’s take a look at what python threads and processes are. Learn about the relevant knowledge of python threads and processes, and understand the differences and advantages and disadvantages of threads and processes in python. We introduced multi-processing and multi-threading, which are the two most common ways to achieve multi-tasking. Now, let’s discuss the pros and cons of both approaches.
First of all, to achieve multi-tasking, we usually design the Master-Worker model. The Master is responsible for allocating tasks and the Worker is responsible for executing tasks. Therefore, in a multi-tasking environment, there is usually one Master and multiple Workers.
If you use multiple processes to implement Master-Worker, the main process is Master, and other processes are Workers.
If you use multi-threading to implement Master-Worker, the main thread is the Master and other threads are the Workers.
The biggest advantage of multi-process mode is its high stability, because if a sub-process crashes, it will not affect the main process and other sub-processes. (Of course, if the master process hangs up, all processes will hang up, but the Master process is only responsible for allocating tasks, and the probability of hanging up is low) The famous Apache first adopted the multi-process mode.
The disadvantage of the multi-process mode is that the cost of creating a process is high. Under Unix/Linux systems, calling fork is okay, but creating processes under Windows is expensive. In addition, the number of processes that the operating system can run at the same time is also limited. Under the limitations of memory and CPU, if there are thousands of processes running at the same time, the operating system will even have scheduling problems.
Multi-threaded mode is usually a little faster than multi-process, but not much faster. Moreover, the fatal disadvantage of multi-threaded mode is that any thread hanging may directly cause the entire process to crash, because all threads share Process memory. On Windows, if there is a problem with the code executed by a thread, you can often see this prompt: "The program has performed an illegal operation and will be closed soon." In fact, there is often a problem with a certain thread, but the operating system will force End the entire process.
Under Windows, multi-threading is more efficient than multi-process, so Microsoft's IIS server adopts multi-threading mode by default. Due to stability issues with multi-threading, IIS is not as stable as Apache. In order to alleviate this problem, IIS and Apache now have a mixed mode of multi-process and multi-thread, which really makes the problem more and more complicated.
Thread switching
Whether it is multi-process or multi-thread, as long as the number is large, the efficiency will definitely not increase. Why?
Let’s use an analogy. Suppose you are unfortunately preparing for the high school entrance examination and need to do homework in five subjects: Chinese, mathematics, English, physics, and chemistry every night. Each homework takes 1 hour.
If you first spend 1 hour doing Chinese homework, and then spend 1 hour doing math homework, and then finish them all in sequence, spending a total of 5 hours, this method is called a single-task model, or a batch model. Process task model.
Suppose you plan to switch to the multi-tasking model, you can first do Chinese for 1 minute, then switch to math homework, do 1 minute, then switch to English, and so on, as long as the switching speed is fast enough, this way It is the same as a single-core CPU performing multitasking. From the perspective of a kindergarten child, you are writing homework for 5 subjects at the same time.
However, there is a price for switching homework. For example, when switching from Chinese to mathematics, you must first clear away the Chinese books and pens on the table (this is called saving the scene), then open the mathematics textbook and find the compass and ruler. (This is called preparing for the new environment) before you can start doing math homework. The operating system is the same when switching processes or threads. It needs to first save the current execution environment (CPU register state, memory pages, etc.), and then prepare the execution environment for the new task (restore the last register state, switch memory page, etc.) before execution can begin. Although this switching process is fast, it also takes time. If there are thousands of tasks running at the same time, the operating system may be mainly busy switching tasks, and there is not much time to perform tasks. The most common situation in this situation is that the hard disk beeps wildly, there is no response when clicking on the window, and the system is in a state of suspended animation.
Therefore, once the number of multitasking reaches a limit, all the resources of the system will be consumed. As a result, the efficiency will drop sharply and all tasks will not be completed well.
Computing intensive vs. IO intensive
The second consideration in whether to use multitasking is the type of task. We can divide tasks into computing-intensive and IO-intensive.
Computing-intensive tasks are characterized by a large amount of calculations that consume CPU resources, such as calculating pi, high-definition decoding of videos, etc., all relying on the computing power of the CPU. Although this kind of computing-intensive task can also be completed with multi-tasking, the more tasks there are, the more time spent on task switching, and the lower the efficiency of the CPU in executing tasks. Therefore, to make the most efficient use of the CPU, computing-intensive tasks The number of simultaneous tasks should be equal to the number of CPU cores.
Computing-intensive tasks mainly consume CPU resources, so code running efficiency is crucial. Scripting languages like Python run very inefficiently and are completely unsuitable for computationally intensive tasks. For computationally intensive tasks, it is better to write in C language.
The second type of task is IO-intensive. Tasks involving network and disk IO are all IO-intensive tasks. The characteristic of this type of task is that the CPU consumption is very small, and most of the time of the task is Wait for the IO operation to complete (because the speed of IO is much slower than the speed of CPU and memory). For IO-intensive tasks, the more tasks, the higher the CPU efficiency, but there is a limit. Most common tasks are IO-intensive tasks, such as web applications.
During the execution of IO-intensive tasks, 99% of the time is spent on IO, and very little time is spent on the CPU. Therefore, use the extremely fast C language to replace the extremely slow running speed of Python. The scripting language cannot improve operating efficiency at all. For IO-intensive tasks, the most suitable language is the language with the highest development efficiency (the least amount of code). Scripting language is the first choice, and C language is the worst.
Asynchronous IO
Considering the huge speed difference between CPU and IO, a task spends most of the time waiting for IO operations during execution. The single-process single-thread model will lead to other problems. Tasks cannot be executed in parallel, so we need a multi-process model or a multi-thread model to support the concurrent execution of multiple tasks.
Modern operating systems have made huge improvements to IO operations. The biggest feature is that they support asynchronous IO. If you make full use of the asynchronous IO support provided by the operating system, you can use a single-process single-thread model to perform multiple tasks. This new model is called an event-driven model. Nginx is a web server that supports asynchronous IO. It runs on a single-core CPU. Using a single-process model can efficiently support multitasking. On a multi-core CPU, you can run multiple processes (the number is the same as the number of CPU cores), taking full advantage of the multi-core CPU. Since the total number of processes in the system is very limited, operating system scheduling is very efficient. Using the asynchronous IO programming model to implement multitasking is a major trend.
Corresponding to the Python language, the single-threaded asynchronous programming model is called coroutine. With the support of coroutine, efficient multi-task programs can be written based on event-driven.
The above is all the content of this article. This article mainly introduces the differences and advantages and disadvantages of threads and processes in python. I hope you can use the information to understand the above. Content. I hope what I have described in this article will be helpful to you and make it easier for you to learn python.
For more related knowledge, please visit thePython tutorial column on the php Chinese website.
The above is the detailed content of The differences and advantages and disadvantages of threads and processes in python. For more information, please follow other related articles on the PHP Chinese website!

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

Python's real-world applications include data analytics, web development, artificial intelligence and automation. 1) In data analysis, Python uses Pandas and Matplotlib to process and visualize data. 2) In web development, Django and Flask frameworks simplify the creation of web applications. 3) In the field of artificial intelligence, TensorFlow and PyTorch are used to build and train models. 4) In terms of automation, Python scripts can be used for tasks such as copying files.

Python is widely used in data science, web development and automation scripting fields. 1) In data science, Python simplifies data processing and analysis through libraries such as NumPy and Pandas. 2) In web development, the Django and Flask frameworks enable developers to quickly build applications. 3) In automated scripts, Python's simplicity and standard library make it ideal.

Python's flexibility is reflected in multi-paradigm support and dynamic type systems, while ease of use comes from a simple syntax and rich standard library. 1. Flexibility: Supports object-oriented, functional and procedural programming, and dynamic type systems improve development efficiency. 2. Ease of use: The grammar is close to natural language, the standard library covers a wide range of functions, and simplifies the development process.

Python is highly favored for its simplicity and power, suitable for all needs from beginners to advanced developers. Its versatility is reflected in: 1) Easy to learn and use, simple syntax; 2) Rich libraries and frameworks, such as NumPy, Pandas, etc.; 3) Cross-platform support, which can be run on a variety of operating systems; 4) Suitable for scripting and automation tasks to improve work efficiency.

Yes, learn Python in two hours a day. 1. Develop a reasonable study plan, 2. Select the right learning resources, 3. Consolidate the knowledge learned through practice. These steps can help you master Python in a short time.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment