Python3.12之前因为GIL的限制,CPU没有真正的多线程,使用threading不过是提高了I/O密集型运算的速度
多线程
import time
import threading
def my_function(seconds):
time.sleep(seconds)
start_time = time.perf_counter()
thread1 = threading.Thread(target=my_function, args=[1])
thread2 = threading.Thread(target=my_function, args=[1])
thread1.start()
thread2.start()
thread1.join()
thread2.join()
finish_time = time.perf_counter()
print("Finished in {} seconds".format(finish_time - start_time))
线程池
import time
import concurrent.futures
def my_function(seconds):
time.sleep(seconds)
return f"完成睡眠{seconds}"
start_time = time.perf_counter()
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [executor.submit(my_function, 1) for _ in range(10)]
for future in concurrent.futures.as_completed(futures):
print(future.result())
finish_time = time.perf_counter()
print("Finished in {} seconds".format(finish_time - start_time))
如果需要提高CPU并发的速度,推荐使用多进程
多进程,注意要加上if name == ‘main’:,否则代码会从上往下执行,重复创建进程
import time
import concurrent.futures
import multiprocessing
def my_function(seconds):
time.sleep(seconds)
if __name__ == '__main__':
start_time = time.perf_counter()
p1 = multiprocessing.Process(target=my_function, args=[1])
p2 = multiprocessing.Process(target=my_function, args=[1])
p1.start()
p2.start()
p1.join()
p2.join()
finish_time = time.perf_counter()
print("Finished in {} seconds".format(finish_time - start_time))
进程池
import time
import concurrent.futures
import multiprocessing
def my_function(seconds):
time.sleep(seconds)
return f"睡眠{seconds}秒"
if __name__ == '__main__':
start_time = time.perf_counter()
with concurrent.futures.ProcessPoolExecutor() as executor:
futures = [executor.submit(my_function, 1) for _ in range(10)]
for future in concurrent.futures.as_completed(futures):
print(future.result())
finish_time = time.perf_counter()
print("Finished in {} seconds".format(finish_time - start_time))