


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!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
