This commit is contained in:
2025-08-14 17:32:27 +08:00
parent b3bfab1569
commit 5e77dc87e6
3 changed files with 52 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
"""
This code is supported by the website: https://www.guanjihuan.com
The newest version of this code is on the web page: https://www.guanjihuan.com/archives/4536
"""
import multiprocessing
import os
import time
def run_proc(name): # 要执行的代码
start_time = time.perf_counter()
time.sleep(2)
end_time = time.perf_counter()
print ('Process id running on %s = %s' % (name, os.getpid()), '; running time = %s' % (end_time-start_time))
if __name__ == '__main__':
start_time = time.perf_counter()
# 循环创建进程
processes = []
for i in range(4):
p = multiprocessing.Process(target=run_proc, args=(f'job{i}',))
processes.append(p)
p.start()
# 等待所有进程完成
for p in processes:
p.join()
end_time = time.perf_counter()
print('运行时间(s)=', (end_time-start_time))

View File

@@ -0,0 +1,21 @@
"""
This code is supported by the website: https://www.guanjihuan.com
The newest version of this code is on the web page: https://www.guanjihuan.com/archives/4536
"""
import multiprocessing
import os
import time
def run_proc(name):
start_time = time.perf_counter()
time.sleep(2)
end_time = time.perf_counter()
print ('Process id running on %s = %s' % (name, os.getpid()), '; running time = %s' % (end_time-start_time))
if __name__ == '__main__':
start_time = time.time()
with multiprocessing.Pool() as pool:
results = pool.map(run_proc, range(64))
end_time = time.time()
print(end_time - start_time)