测试代码如下:
def processtest( n ):
print('进程'+str(n)+':', datetime.datetime.now())
if __name__ == '__main__':
print('t0=', datetime.datetime.now())
pool = multiprocessing.Pool(processes=2)
print('t1=', datetime.datetime.now())
for i in range(2):
pool.apply_async(processtest, (i,))
print('t2=', datetime.datetime.now())
pool.close()
pool.join()
print('t3=', datetime.datetime.now())
耗时如下:
t0= 2019-08-14 17:24:24.649186
t1= 2019-08-14 17:24:24.679078
t2= 2019-08-14 17:24:24.680076
进程0: 2019-08-14 17:24:29.058592
进程1: 2019-08-14 17:24:29.058592
t3= 2019-08-14 17:24:29.353800
最近做项目,由于业务逻辑比较费时,需要开启进程加速,但是开启进程池的时间消耗太大,是否可以优化?