Python 3 multithreading

What is thread


Thread is the smallest unit that the operating system can perform operation scheduling

Included in the process, is the actual operation unit in the process

Multiple threads can be concurrent in a process, and each thread can perform different tasks

Multithreading is similar to executing multiple different programs at the same time

Advantage 1: Put long tasks in the background for processing

Advantage 2: The program may run faster


Python implements multithreading


Python provides thread and threading modules

threading is more advanced than thread module

Pass in a function and create a Thread instance, call the start method to execute















import threading#定义多线程执行函数def test(name,i):    print(name+'执行:',i)
#创建t1\t2两个线程t1 = threading.Thread(target = test,args=('线程一',100))t2 = threading.Thread(target = test,args=('线程二',200))#执行线程t1.start()t2.start()#线程同步t1.join()t2.join()

image


Thread pool usage


Threads and processes can manage multithreading through the thread pool

ThreadPoolExecutor implements thread pool















from concurrent.futures import ThreadPoolExecutor
#定义多线程执行函数def test(name,i):    print('线程'+name+'执行:',i)
#创建多个线程thre_name = []  #定义线程池变量th_pool = ThreadPoolExecutor(max_workers = 2)    #创建线程池,上限5个for i in range(10):    thre_name.append(i)    thre_name[i] = th_pool.submit(test,(thre_name[i],i))for i in range(0,len(thre_name)):    print('线程%s是否完成'%i,thre_name[i].done())

The done() method is used to determine whether the thread has completed execution


Global Lock (GIL)


GIL generates mutex locks to restrict thread access to shared resources

GIL decides that multiple threads cannot call multiple CPU cores

Multi-threading is not recommended for CPU-intensive operations, multi-process is recommended

IO-intensive operations, multi-threading can significantly improve efficiency

Multithreading and'crawler' can be perfectly combined


Guess you like

Origin blog.51cto.com/15069490/2578650