from multiprocessing import Process
import os
import time
def long_time_task(n):
print('子进程: {} - 任务{}'.format(os.getpid(), n))
time.sleep(2)
# 主要计算任务
print("结果: {}".format(10 ** 200))
print('当前主进程: {}'.format(os.getpid()))
start = time.time()
# 创建子进程
p1 = Process(target=long_time_task, args=(1,))
p2 = Process(target=long_time_task, args=(2,))
# 开始执行子进程
p1.start()
p2.start()
print('等待所有⼦子进程完成。')
p1.join()
p2.join()
end = time.time()
running_time = end - start
print("总共⽤用时{}秒".format(running_time))
08-25
7万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)