Crawler 13 - thread process

1. Overview of the thread process

Every time the operating system runs a program, it will prepare a memory for the program, and then this memory is dedicated to storing the variables generated during the execution of the program.

This memory area can be considered as the xxx process, and then there are several threads in the process to help us work (at least one). So a process is a resource unit, and a thread is an execution unit, and the CPU executes a thread when it is running.

When we create a process, what if we don't create a thread? The answer is of course no. You can compare the process to a company. In order for the company to run, there must be at least one employee (thread). When the program is executed, if one thread is not enough, you need to create a few more threads. This is the concept of multithreading.

Every time a program is created, there is a main thread in it (main)

2. A case is given below to see if it is a multi-threaded program

def func():
    for i in range(1000):
        print("func",i)
if __name__ == '__main__':
    func()
    for i in range(1000):
        print("main",i)

By observing the process of program execution, when the program is executed, first generate func, and then return to the func function in the main thread to execute, and then return to main and then continue to execute, so this is a line The process, single-threaded, not multi-threaded.

It can also be seen from the running results that func is executed first and then main.

 

3. Two creation methods of multithreading

Method 1: import package Thread

# 进程,线程
# 进程是资源单位,每一个进程至少有一个线程
# 线程是执行单位

# 启动每一个程序默认都会有一个主线程

#多线程
from threading import Thread # 线程类
def func():
    for i in range(1000):
        print("func",i)

if __name__ == '__main__':
    #创建线程类对象,并告诉程序你当前的线程,如果要执行的话,要执行谁 。
    #target=后面的子线程不能加括号 加括号相当于返回值做参数,不加括号相当于函数本身做参数。
    T = Thread(target=func) #创建线程,并给线程分配任务,
    '''
    Thread相当与公司招了一个新员工,新线程
    target=func 相当于告诉新员工,如果你开始执行,你该干什么事情
    同时呢,招完员工后自己该干什么事继续去干
    '''
    T.start() #多线程的状态为可以开始工作状态,具体的执行时间还得由CPU决定
    for i in range(1000):
        print("main",i)

Through the running results, it is found that the func and main threads are executed together, because the printing places are all on the console, so there will be overlapping processes.

Method 2: Create the class MyThread yourself

from threading import Thread # 线程类
class MyThread(Thread): #继承Thread
    '''
    MyThread是我们自己创建的类,我们的类继承Thread就相当于是Thread的子类
    所以我们写的类就会有Thread的特性
    (子承父业)
    '''
    # 重写Thread里的run方法
    def run(self): #当线程被执行的时候,被执行的就是run()
        for i in range(1000):
            print("子线程", i)
if __name__ == '__main__':
    T = MyThread()
    #T.run() #方法的调用 单线程
    T.start() #开启线程
    for i in range(1000):
        print("主线程",i)

The result is also the same. 

4. Multi-thread creation (parameter passing)

When we need to pass in two threads, it is difficult to see which is which in the following way.

from threading import Thread
def func():
    for i in range(1000):
        print("func",i)
if __name__ == '__main__':
    T1 = Thread(target=func)
    T1.start()

    T2 = Thread(target=func)
    T2.start()

【operation result】

The display is all func, I don’t know which one is t1 and t2 respectively

At this time, you need to use the method of passing parameters. You only need to add a parameter (args) to confirm the name of the thread when passing in the thread. It should be noted that the value corresponding to args must be a tuple.

from threading import Thread
def func(name):
    for i in range(1000):
        print(name,i)
if __name__ == '__main__':
    T1 = Thread(target=func,args=("王力宏",)) # 传入参数必须是元组的形式
    T1.start()

    T2 = Thread(target=func,args=("周杰伦",))
    T2.start()

In this way, the name of each thread can be clearly known. 

 5. The creation of multiple processes

Compared with multi-threading, multi-process will use a little less, because opening up a process requires more resources than opening up a thread, because it needs to open up memory.

The creation of multi-process is similar to multi-threading, but the imported packages are different

from multiprocessing import Process #进程类
def func():
    for i in range(20000):
        print("子进程",i)
if __name__ == '__main__':
    P = Process(target=func)
    P.start()
    for i in range(20000):
        print("主进程",i)

【operation result】

 

Guess you like

Origin blog.csdn.net/m0_48936146/article/details/124748533