Little computer employee-multithreading

In the last two articles, we talked about multitasking and multithreading. We vividly compared the two to a large factory and a small workshop. However, a factory only has resources and a workshop. It must also have workers working at work, that is The thread we want to understand today

Thread

Thread (thread) is the smallest unit that the operating system can perform operation scheduling. It is included in the process and is the actual operation unit in the process. A thread refers to a single sequential control flow in a process, and a process can be concurrent. Multiple threads, each thread executes different tasks in parallel. Python's threading module wraps threads, which can be used more conveniently. The threads are workers in the workshop, and the workers who actually work are also workers. Thread
threads are also divided into single thread and multi thread

Single thread

The execution of the program in a single thread is still executed line by line from top to bottom according to the plan. The calling function we usually use is a manifestation of single thread.
Still take upload and download as an example

import time
def upload():
    print("开始上传。。。")
    for i in range(1,5):
        print(f"上传了{'{:.2%}'.format(i / 4)}")
        time.sleep(1)
    print("上传完毕。。。")

def download():
    print("开始下载.......")
    for i in range(1,5):
        print(f"下载了{'{:.2%}'.format(i / 4)}")
        time.sleep(1)
    print("下载完毕.......")

upload()
download()

Multithreading

Insert picture description hereWe then use cases to further multi-threaded

import time
import threading

def upload():
    print("开始上传.......")
    for i in range(1, 5):
        print(f"上传了{'{:.2%}'.format(i / 4)}")
        time.sleep(1)
    print("上传结束.......")

def download():
    print("开始下载.......")
    for i in range(1, 5):
        print(f"下载了{'{:.2%}'.format(i / 4)}")
        time.sleep(1)
    print("下载完毕.......")

def main():
    upload_thread=threading.Thread(target=upload)
    down_thread=threading.Thread(target=download)
    upload_thread.start()
    down_thread.start()

if __name__ == '__main__':
    main()

Seeing this, we have to understand that there must be a thread for the execution of a program, and this thread is the main thread, and there must be a problem. The execution of a program must have a process, and this process is the main process.

When we want to see the number of threads, we can use the threading.enumerate() method

Characteristics of threads

1. Encapsulation of thread execution code.
Multitasking program development can be completed by using threading module. In order to make the encapsulation of each thread more perfect, when threading module is used, a new subclass is often customized, as long as threading is inherited. Thread can be implemented. Under normal circumstances, the entry function in a thread is run
2. Multi-threaded shared global variables
When modifying global variables in a function, whether it is necessary to see whether the execution of global variables is referenced or modified. If the reference is modified, which means that the global variable points to a new place, if only the referenced data is modified, there is no need to worry about the variable being divided at this time

Guess you like

Origin blog.csdn.net/Layfolk_XK/article/details/107998605