'''
多进程编程
# Pythong中由于有GIL锁的存在,服务发挥CPU多核的优势,多线程无法并发,充分利用多核,但多进程可以。
# 对于IO操作时,瓶颈不在CPU,使用多线程编程
# 进程切换的代价高于线程,因此能用多线程进来使用多线程。
#1. 对于耗费CPU的操作,多进程优于多线程
'''''
from concurrent.futures import ThreadPoolExecutor,as_completed
import time
def fib(n):
if n <= 2:
return 1
return fib(n-1) + fib(n-2)
'''多线程操作'''
with ThreadPoolExecutor(3) as executer:
all_task = [executer.submit(fib,(num)) for num in range(25,35)]
start_time = time.time()
for future in as_completed(all_task):
data = future.result()
print("exe result: {}".format(data))
print("last time is: {}".format(time.time()-start_time))
exe result: 75025
exe result: 121393
exe result: 196418
exe result: 317811
exe result: 514229
exe result: 832040
exe result: 1346269
exe result: 2178309
exe result: 3524578
exe result: 5702887
last time is: 5.594969987869263
from concurrent.futures import ThreadPoolExecutor,as_completed
from concurrent.futures import ProcessPoolExecutor
import time
def fib(n):
if n <= 2:
return 1
return fib(n-1) + fib(n-2)
# '''多进程编程,后面的执行语句必须放到”if __name__ == "__main__": 后面'''
if __name__ == "__main__":
with ProcessPoolExecutor(3) as executer:
all_task = [executer.submit(fib,(num)) for num in range(25,35)]
start_time = time.time()
for future in as_completed(all_task):
data = future.result()
print("exe result: {}".format(data))
print("last time is: {}".format(time.time()-start_time))
exe result: 75025
exe result: 121393
exe result: 196418
exe result: 317811
exe result: 514229
exe result: 832040
exe result: 1346269
exe result: 2178309
exe result: 3524578
exe result: 5702887
last time is: 4.888859987258911
通过对比可以发现,多余耗费大量计算操作的操作,多进程操作比多线程操作更快。
性能没有翻倍? 因为多进程的进程切换,比多线程的线程切换更耗时。
import time
from concurrent.futures import ThreadPoolExecutor,as_completed
from concurrent.futures import ProcessPoolExecutor
'''
# 对于io操作来说,多线程优于多进程。
'''
def random_sleep(n):
time.sleep(n)
return n
if __name__ == "__main__":
with ProcessPoolExecutor(3) as executer:
all_task = [executer.submit(random_sleep,(num)) for num in [2]*30] # 执行30次等待2秒
start_time = time.time()
for future in as_completed(all_task):
data = future.result()
print("exe result: {}".format(data))
print("last time is: {}".format(time.time()-start_time))
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
last time is: 20.831419944763184
import time
from concurrent.futures import ThreadPoolExecutor,as_completed
from concurrent.futures import ProcessPoolExecutor
# 对于io操作来说,多线程优于多进程。
def random_sleep(n):
time.sleep(n)
return n
if __name__ == "__main__":
with ThreadPoolExecutor(3) as executer:
all_task = [executer.submit(random_sleep,(num)) for num in [2]*30] # 执行30次等待2秒
start_time = time.time()
for future in as_completed(all_task):
data = future.result()
print("exe result: {}".format(data))
print("last time is: {}".format(time.time()-start_time))
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
exe result: 2
last time is: 20.146894216537476
通过上面示例,虽然领先不多,但也可说明多线程比多进程稍微快一点。
对于操作系统,开放的线程数高于进程数,尽量使用多线程。