标签:def __name__ 多进程 imp star seconds range now() ota
multiprocessing多进程代码演示主要是和多线程在CPU密集型操作时的所耗时间对比
import multiprocessing
import datetime
def calc(i):
sum = 0
for _ in range(1000):
sum += 1
print(i, sum)
if __name__ == ‘__main__‘:
start = datetime.datetime.now()
ps = []
for i in range(5):
p = multiprocessing.Process(target=calc, args=(i, ), name=‘calc-{}‘.format(i))
ps.append(p)
p.start()
for p in ps:
p.join()
delta = (datetime.datetime.now() - start).total_seconds()
print(delta)
标签:def __name__ 多进程 imp star seconds range now() ota
原文地址:https://blog.51cto.com/windchasereric/2358069