01、多线程 多进程之间区别 -- 演示cpu消耗大的代码,使用进程更加合算
--多线程主要是针对高并发(io 爬虫) 和 不消耗CPU的操作
--多进程主要是对消耗CPU的操作 (至于涉及到系统资源切换,线程比进程要差很多)
--1、计算
--2、
--关于多进程,多线程的使用,这里concurrent包使用起来非常方便省事。只需更改一下类即可
import time
# 斐波拉契亚数列检验多进程计算比多线程块
def fib(n):
if n <= 2:
return 1
return fib(n-1) + fib(n-2)
# 多线程调用, 这里暂时不放在 if __name__ == '__main__':中,但是一般必须放在这里面否则报错【多进程尤甚】
from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED, as_completed
start_time_01 = time.time()
with ThreadPoolExecutor(5) as executor:
all_tasks_01 = [executor.submit(fib, (num)) for num in range(25, 40)]
for future in as_completed(all_tasks_01):
data = future.result()
print('计算结果为:{}'.format(data))
print('多线程运行时间为:{}'.format(str(time.time() - start_time_01))) # 25s
"""
# 多进程调用, 需要放在if __name__ == '__main__': 下,当然了linux不需要
from concurrent.futures import ProcessPoolExecutor, as_completed
if __name__ == '__main__':
start_time_02 = time.time()
with ProcessPoolExecutor(5) as executor:
all_tasks_01 = [executor.submit(fib, (num)) for num in range(25, 40)]
for future in as_completed(all_tasks_01):
data = future.result()
print('计算结果为:{}'.format(data))
print('多进程运行时间为:{}'.format(str(time.time() - start_time_02))) # 13s
"""
02、多线程 多进程之间区别 -- 演示io消耗大的代码,使用线程更加合算【这里切换差异不是非常大,但是足以说明问题】
import time
def random_sleep(n):
time.sleep(n)
return n
"""
# 多线程调用, 这里暂时不放在 if __name__ == '__main__':中,但是一般必须放在这里面否则报错【多进程尤甚】
from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED, as_completed
start_time_01 = time.time()
with ThreadPoolExecutor(5) as executor:
all_tasks_01 = [executor.submit(random_sleep, (num)) for num in [2]*30]
for future in as_completed(all_tasks_01):
data = future.result()
print('计算结果为:{}'.format(data))
print('多线程运行时间为:{}'.format(str(time.time() - start_time_01))) # 12.012s
"""
# 多进程调用, 需要放在if __name__ == '__main__': 下,当然了linux不需要
from concurrent.futures import ProcessPoolExecutor, as_completed
if __name__ == '__main__':
start_time_02 = time.time()
with ProcessPoolExecutor(5) as executor:
all_tasks_01 = [executor.submit(random_sleep, (num)) for num in [2]*30]
for future in as_completed(all_tasks_01):
data = future.result()
print('计算结果为:{}'.format(data))
print('多进程运行时间为:{}'.format(str(time.time() - start_time_02))) # 12.221s