IO process pool without clogging case
# Coding: UTF. 8-
Import Time
from multiprocessing Import Process, Pool
DEF FUNC (n-):
Pass
IF the __name__ == ' __main__ ' :
NUM = 10
start_pool_time = the time.time ()
the pool = Pool (. 5 )
pool.map (FUNC , the Range (NUM)) # the Map is asynchronous, and comes close and the Join
Print ( " time to perform the process by the pool: " , time.time () - start_pool_time)
std_start_time = time.time ()
forI in : (NUM) Range
Pass
Print ( " execution time normally performed: " , the time.time () - std_start_time)
pro_start_time = the time.time ()
p_lst = []
for I in (NUM) Range:
P = Process ( FUNC = target, args = (i,))
p.start ()
p_lst.append (the p-)
[pp.join () for PP in p_lst]
Print ( " execution time multi-process: " , time.time () - pro_start_time)
#By the time of the execution process pool: 0.46875
# normal execution execution time: 0.0
# execution time multi-process: 0.828125
# general convention of the process is the number of process pool for the number of CPU's work depends on the specific circumstances to consider.
There are circumstances IO blocked
# coding:utf-8
import time
from multiprocessing import Process, Pool
def func(n):
time.sleep(1)
if __name__ == '__main__':
num = 10
start_pool_time = time.time()
pool = Pool(5)
pool.map(func, range(num))
print("通过进程池执行的时间:", time.time() - start_pool_time)
std_start_time = time.time()
for i in range(num):
time.sleep(1)
print("正常执行的执行时间:", time.time() - std_start_time)
pro_start_time = time.time()
p_lst = []
for i in range(num):
p = Process(target=func, args=(i,))
p.start()
p_lst.append(p)
[pp.join() for pp in p_lst]
print("多进程的执行时间:", time.time() - pro_start_time)
# 通过进程池执行的时间: 2.578125
# 正常执行的执行时间: 10.0
# 多进程的执行时间: 1.75