全局锁 一般一个程序可以理解为CPU计算加IO操作 CPU计算是计算密集型,IO操作是IO密集型 多线程 利用CPU计算和IO可以同时进行来加速程序运行 适用于IO密集型操作 多进程 利用CPU的多个核去同时执行实现正真的并行 适用于CPU密集型操作
GIL :全局解释器锁 是一个互斥锁 同一时刻只能有一个线程执行代码 导致多线程无法并行执行
多线程 threading 模块 1 2 3 4 5 6 7 8 9 import threadingdef task (a, b ): print ("Thread is running" ) t = threading.Thread(target=task, args=(1 , 2 )) t.start() t.join()
example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import threadingimport timedef task (a, b ): print (f"Thread{a} {b} is running" ) time.sleep(2 ) t1 = threading.Thread(target=task, args=(1 , 2 )) t2 = threading.Thread(target=task, args=("a" , "b" )) if __name__ == "__main__" : start_time = time.time() t1.start() t2.start() t1.join() t2.join() end_time = time.time() print (f"Total execution time: {end_time - start_time} seconds" )
线程池 ThreadPoolExecutor map方式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 from concurrent.futures import ThreadPoolExecutorimport timedef task (a, b ): print (f"Thread{a} {b} is running" ) time.sleep(2 ) return str (a+b) if __name__ == "__main__" : start_time = time.time() with ThreadPoolExecutor(max_workers=2 ) as executor: rets = executor.map (task, [1 , "a" ], [2 , "b" ]) for ret in rets: print (ret) end_time = time.time() print (f"Total execution time: {end_time - start_time} seconds" )
submit方式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 from concurrent.futures import ThreadPoolExecutorimport timedef task (a, b ): print (f"Thread{a} {b} is running" ) time.sleep(2 ) return str (a+b) if __name__ == "__main__" : start_time = time.time() with ThreadPoolExecutor(max_workers=2 ) as executor: futures = [executor.submit(task, *args) for args in [(1 ,2 ), ("a" ,"b" )]] for future in futures: future.result() end_time = time.time() print (f"Total execution time: {end_time - start_time} seconds" )
多进程 multiprocessing 模块 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 import timefrom multiprocessing import Processdef count_primes (start, end ): count = 0 for i in range (start, end): if i > 1 : for j in range (2 , i): if (i % j) == 0 : break else : count += 1 return count if __name__ == "__main__" : start_time = time.time() num_primes = count_primes(1 , 100000 ) end_time = time.time() print (f"excutor time {end_time - start_time} second" ) start_time = time.time() p1 = Process(target=count_primes, args=(1 , 50000 )) p1.start() p1.join() p2 = Process(target=count_primes, args=(50000 , 100000 )) p2.start() p2.join() end_time = time.time() print (f"excutor time {end_time - start_time} second" )
进程池 ProcessPoolExecutor map方式 1 2 3 4 5 6 7 8 9 with ProcessPoolExecutor() as executor: start_time = time.time() num_primes = list (executor.map (count_primes, [1 , 50000 ], [50000 , 100000 ])) end_time = time.time() print (f"excutor time {end_time - start_time} second" )
submit方式 1 2 3 4 5 6 7 8 9 with ProcessPoolExecutor() as executor: start_time = time.time() futures = [executor.submit(count_primes, 1 , 50000 ), executor.submit(count_primes, 50000 , 100000 )] for future in as_completed(futures): num_primes = future.result() end_time = time.time() print (f"excutor time {end_time - start_time} second" )
要注意是否分配的任务是公平的否则并不能提高效率