import time
import threading
from multiprocessing import Process
from concurrent.futures import ProcessPoolExecutor
def time_decorator(func):
def wrapper(*args, **kwargs):
now = time.time()
func(*args, **kwargs)
print(time.time() - now)
return wrapper
def add():
for i in range(100000000):
pass
@time_decorator
def multi_threading():
t_list = []
for i in range(10):
t = threading.Thread(target=add)
t_list.append(t)
for t in t_list:
t.start()
for t in t_list:
t.join()
@time_decorator
def multi_processing():
t_list = []
for i in range(10):
t = Process(target=add)
t_list.append(t)
for t in t_list:
t.start()
for t in t_list:
t.join()
@time_decorator
def future_multi_processing():
"""
父子进程, 平行运行多个GIL解释器.
父子进程, 通信通过Pickle序列化及本地套接字(local socket), 序列化和反序列化, 开销较大.
没有任何附加信息进程间通信
"""
r_list = []
pool = ProcessPoolExecutor(max_workers=10)
for i in range(10):
t = pool.submit(add)
r_list.append(t)
pool.shutdown()
@time_decorator
def add_total():
for i in range(100000000 * 10):
pass
if __name__ == "__main__":
multi_threading()
multi_processing()
future_multi_processing()
add_total()
29.84246516227722 # 多线程
14.255790710449219 # 多进程
13.782246112823486 # futures.ProcessPoolExecutor
父子进程, 平行运行多个GIL解释器.
父子进程, 通信通过Pickle序列化及本地套接字(local socket), 序列化和反序列化, 开销较大.
没有任何附加信息进程间通信
26.558323860168457 # 正常执行