Home >Backend Development >Python Tutorial >What are threads in python? Summary of concepts and advantages
In the following article, let’s take a look at what threads in python are. Learn about the python process and what role multi-threadedpython can play in python programming.
Introducing the concepts and differences between processes and threads
1. Basic concepts of threads
Concepts
Thread is the smallest unit that performs operations in the process. It is an entity in the process and the basic unit that is independently scheduled and dispatched by the system. The thread itself does not own system resources and only owns some resources that are essential during operation, but it All resources owned by the process can be shared with other threads belonging to the same process. A thread can create and destroy another thread, and multiple threads in the same process can execute concurrently.
Benefits
(1) Easy to schedule.
(2) Improve concurrency. Concurrency can be achieved easily and efficiently through threads. A process can create multiple threads to execute different parts of the same program.
(3) Low overhead. Creating threads is faster than creating processes, and requires very little overhead.
2. Basic status of the process and the relationship between the states
Status: running, blocked, suspended blocked, ready, hung Ready
Transition between states:
(1) The ready process is scheduled to be executed by the CPU and becomes the running state;
(2) Running A process that makes an I/O request or cannot obtain the requested resource becomes blocked;
(3) A running process becomes ready after the process execution is completed (or the time slice has expired);
(4) Suspend the blocked process into the suspended blocked state. When the I/O operation that caused the process to be blocked is completed before the user restarts the process (called wake-up), the suspended blocked state It becomes the suspended ready state. When the user restarts the process before the I/O operation is completed, the suspended blocking state becomes the blocking state;
(5) Suspend the process in ready (or running) and become the blocking state. When the process resumes, the suspended ready state becomes ready;
3. What is the relationship and difference between threads and processes?
The relationship between processes and threads:
(1) A thread can only belong to one process, and a process can have multiple threads, but there is at least one thread.
(2) Resources are allocated to processes, and all threads of the same process share all resources of the process.
(3) The processor is allocated to threads, that is, the threads are actually running on the processor.
(4) During the execution of threads, collaborative synchronization is required. The threads of different processes must use message communication to achieve synchronization. A thread refers to an execution unit within a process and is also a schedulable entity within the process.
The difference between a process and a thread:
(1) Scheduling: Thread is the basic unit of scheduling and allocation. Process is the basic unit of resource ownership
(2) Concurrency: not only processes can be executed concurrently, but multiple threads of the same process can also be executed concurrently
(3) Ownership Resources: A process is an independent unit that owns resources. A thread does not own system resources, but can access resources belonging to the process.
(4) System overhead: When creating or canceling a process, the system must Allocating and recycling resources causes the system overhead to be significantly greater than the overhead when creating or canceling threads.
4. How to communicate between processes?
(1) Pipe and named pipe: Pipes can be used for communication between parent and child processes that have an affinity. In addition to having the functions of a pipe, a named pipe also allows unrelated Communication between relational processes.
(2) Signal: Signal is a simulation of the interrupt mechanism at the software level. It is a relatively complex communication method used to notify the process that a certain event has occurred. A process receives an The effect of the signal and the processor receiving an interrupt request can be said to be consistent.
(3) Message queue (message queue): The message queue is a linked list of messages. It overcomes the shortcomings of limited semaphores in the previous two communication methods. Processes with write permissions can send messages to the message queue according to certain rules. New information is added to the message queue; processes with read permissions on the message queue can read information from the message queue.
(4) Shared memory: It can be said that this is the most useful inter-process communication method. It allows multiple processes to access the same memory space, and different processes can see updates to the data in the shared memory in each other's process in a timely manner. This method requires some kind of synchronization operation, such as mutex locks and semaphores.
(5) Semaphore: Mainly used as a means of synchronization and mutual exclusion between processes and between different threads of the same process.
(6) Socket: This is a more general inter-process communication mechanism. It can be used for inter-process communication between different machines in the network and is widely used.
5. The difference between synchronization and mutual exclusion:
When there are multiple threads, it is often necessary to synchronize these threads to access the same data or resources. For example, suppose there is a program in which one thread is used to read a file into memory, and another thread is used to count the number of characters in the file. Of course, there's no point in counting the entire file until it's been loaded into memory. However, since each operation has its own thread, the operating system treats the two threads as independent tasks and executes them separately, making it possible to count the words without loading the entire file into memory. To solve this problem, you must make both threads work synchronously.
The so-called synchronization refers to several program fragments walking between different processes. Their operation must be run in strict accordance with a certain specified order. This order depends on the specific tasks to be completed. If defined in terms of access to resources, synchronization refers to the orderly access of resources by visitors through other mechanisms on the basis of mutual exclusion (in most cases). In most cases, synchronization already implements mutual exclusion, especially when all writes to resources must be mutually exclusive. In rare cases, multiple visitors can be allowed to access the resource at the same time.
The so-called mutual exclusion refers to several program fragments scattered among different processes. When a process runs one of the program fragments, other processes cannot run any of them. They can only Wait until the process has finished running this program fragment before running it. If defined by access to resources, a mutually exclusive resource allows only one visitor to access it at the same time, which is unique and exclusive. But mutual exclusion cannot limit the order in which visitors access resources, that is, access is unordered.
The above is all the content of this article. This article mainly introduces the relevant knowledge of threads in python. I hope You can use the information to understand what is said above. 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 the
Python tutorialcolumn on the php Chinese website.
The above is the detailed content of What are threads in python? Summary of concepts and advantages. For more information, please follow other related articles on the PHP Chinese website!