Update parallel_calculations_with_python_using_Pool.py

This commit is contained in:
2025-08-14 17:59:34 +08:00
parent 5e77dc87e6
commit cb35e77c38

View File

@@ -12,10 +12,12 @@ def run_proc(name):
time.sleep(2) time.sleep(2)
end_time = time.perf_counter() end_time = time.perf_counter()
print ('Process id running on %s = %s' % (name, os.getpid()), '; running time = %s' % (end_time-start_time)) print ('Process id running on %s = %s' % (name, os.getpid()), '; running time = %s' % (end_time-start_time))
return name
if __name__ == '__main__': if __name__ == '__main__':
start_time = time.time() start_time = time.time()
with multiprocessing.Pool() as pool: with multiprocessing.Pool() as pool:
results = pool.map(run_proc, range(64)) results = pool.map(run_proc, [f"task {i}" for i in range(64)])
end_time = time.time() end_time = time.time()
print(results)
print(end_time - start_time) print(end_time - start_time)