Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The concept of Thread and process in Python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "the concept of threads and processes in Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The basic concept of process

Concept

A process is a dynamic execution of a program on a dataset. The process generally consists of three parts: the program, the data set and the process control block. The program we write is used to describe what functions the process needs to complete and how to complete it; the dataset is the resource that the program needs to use in the process of execution; the process control block is used to record the external characteristics of the process and describe the changing process of the process. the system can use it to control and manage the process, which is the only sign that the system perceives the existence of the process.

The basic concept of threading

Concept

A thread is the smallest unit to perform operations in a process, an entity in a process, and a basic unit that is independently scheduled and dispatched by the system. The thread itself does not own system resources, but only has some indispensable resources in operation. However, it can share all the resources owned by the process with other threads belonging to the same process. One thread can create and undo another thread, and multiple threads in the same process can execute concurrently.

Benefits

Easy to dispatch.

Improve concurrency, and concurrency can be realized conveniently and effectively through threads. A process can create multiple threads to execute different parts of the same program.

Less overhead, faster to create a thread than to create a process, and very little overhead

The basic state of a process and the relationship between it

Status: running, blocking, pending blocking, ready, pending ready

Transitions between states:

A process that is ready, scheduled and executed by CPU, becomes running

The running process, which makes an Igamot request or cannot get the requested resource, becomes blocked.

A running process that finishes execution (or time slices have expired) and becomes ready

The process in the blocking state is suspended into the suspended blocking state. When the Icano operation that causes the blocking of the process is completed before the user restarts the process (called wake-up), the suspended blocking state becomes the suspended ready state, and when the user restarts the process before the IWeiO operation ends, the suspended blocking state becomes the blocked state.

Suspend a process that is ready (or running) into a pending ready state, and when the process resumes, the suspended ready state becomes ready

The relationship between processes and threads:

A thread can only belong to one process, and a process can have multiple threads, but at least one thread.

Resources are assigned to a process, and all threads of the same process share all resources of that process.

The processor is allocated to the thread, that is, what is really running on the processor is the thread

In the process of execution, threads need to cooperate and synchronize. The threads of different processes should use the method of message communication to achieve synchronization. A thread is an execution unit within a process and a schedulable entity within a process.

The difference between processes and threads:

Different modes of operation

A process cannot be executed alone, it is just a collection of resources.

To operate on CPU, a process must first create a thread.

All threads in the same process share the memory space occupied by the same process.

Relationship

The first thread in the process is the main thread, which can create other threads; other threads can also create threads; and the threads are equal.

A process has a parent process and a child process, an independent memory space, and a unique identifier: pid.

Speed

Starting a thread is faster than starting a process

The running thread and the running process are the same in speed, and there is no comparability.

Threads share memory space, and the memory of the process is independent.

Create

The parent process generates child processes, which is equivalent to copying a memory space, and cannot be accessed directly between processes.

Creating a new thread is simple. Creating a new process requires a copy of the parent process.

A thread can control and manipulate other threads in a peer thread, but a process can only operate on child processes.

Interaction

There is direct access between threads in the same process.

The communication between two processes must be achieved through an intermediate agent.

Dispatching

Thread as the basic unit of scheduling and allocation, and process as the basic unit of owning resources.

Concurrency

It can be executed concurrently not only between processes, but also among multiple threads of the same process.

Own the resources:

A process is an independent unit that owns resources. Threads do not own system resources, but can access resources belonging to the process.

System overhead:

When creating or undoing a process, because the system allocates and reclaims resources for it, the overhead of the system is significantly greater than that of creating or undoing threads.

This is the end of the introduction to the concept of threads and processes in Python. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report