原有代码
def add(a: int, b: int):
print(a + b)
if __name__ == '__main__':
for i in range(300):
add(i, i + 1)
多进程
import multiprocessing
def add(a: int, b: int):
print(a + b)
if __name__ == '__main__':
pool = multiprocessing.Pool(2) # 两个进程执行
# pool = multiprocessing.Pool(multiprocessing.cpu_count()) # 全部cpu执行
for i in range(300):
pool.apply_async(func=add,args=(i, i + 1))
pool.close()
pool.join()