Python Basic Grammar 23-Multithreading Theory

1. Overview
A thread is the smallest unit that an operating system can perform operation scheduling. It is contained in the process, and a process can run multiple threads. Multithreading is similar to executing multiple different programs at the same time. Advantages of multithreading:
(1) Using threads can put tasks in programs that take up a long time in the background for processing.
(2) The user interface can be more attractive. For example, when the user clicks a button to trigger the processing of certain events, a progress bar can pop up to display the progress of the processing.
(3) The running speed of the program may be accelerated.
(4) In the implementation of some waiting tasks, such as user input, file reading and writing, and network sending and receiving data, threads are more useful. In this case we can release some precious resources such as memory usage and so on.
Each independent thread has an entry point for program execution, a sequential execution sequence, and an exit point for the program. However, threads cannot be executed independently, and must depend on the application program, and the application program provides multiple thread execution control.
Each thread has its own set of CPU registers, called the thread's context, which reflects the state of the CPU registers the last time the thread ran the thread.
The instruction pointer and stack pointer registers are the two most important registers in the thread context. The thread always runs in the context of the process. These addresses are used to mark the memory in the process address space that owns the thread.
Threads can be preempted (interrupted).
Threads can be temporarily put on hold (also known as sleeping) while other threads are running -- this is thread yielding.
Threads can be divided into:
kernel threads: created and revoked by the operating system kernel.
User thread: A thread implemented in a user program without kernel support.
The two commonly used modules in Python3 threads are:
_thread
threading (recommended)
2. Introduction to threading
1. threading function
Normally, when a Python program starts, the Python interpreter will start a threading._MainThread thread object inherited from threading.Thread as the main thread, so it involves threading.T

Supongo que te gusta

Origin blog.csdn.net/a316495442/article/details/128457311
Recomendado
Clasificación