From 67c535567a72a2dfde137e0b273a4f72dde36426 Mon Sep 17 00:00:00 2001 From: guanjihuan Date: Fri, 12 Apr 2024 23:28:37 +0800 Subject: [PATCH] update --- ...on_of_parallel_calculations_with_python.py | 7 ++-- .../parallel_calculations_with_python.py | 10 +++--- ...el_calculations_with_python_using_Value.py | 10 +++--- ...l_calculations_with_python_using_kwargs.py | 35 +++++++++++++++++++ 4 files changed, 49 insertions(+), 13 deletions(-) create mode 100644 2020.06.01_parallel_calculations_with_python/parallel_calculations_with_python_using_kwargs.py diff --git a/2020.06.01_parallel_calculations_with_python/application_of_parallel_calculations_with_python.py b/2020.06.01_parallel_calculations_with_python/application_of_parallel_calculations_with_python.py index 251b9d7..c989aa5 100644 --- a/2020.06.01_parallel_calculations_with_python/application_of_parallel_calculations_with_python.py +++ b/2020.06.01_parallel_calculations_with_python/application_of_parallel_calculations_with_python.py @@ -3,7 +3,7 @@ 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 """ -from multiprocessing import Process +import multiprocessing import os import time import numpy as np @@ -25,13 +25,14 @@ if __name__ == '__main__': process_array = [] for task_index in range(task_num): parameter_array = guan.preprocess_for_parallel_calculations(parameter_array_all, task_num, task_index) - process_array.append(Process(target=main, args=(parameter_array, task_index))) + process_array.append(multiprocessing.Process(target=main, args=(parameter_array, task_index))) for process in process_array: # 运行子进程 process.start() for process in process_array: # 等待子进程完成 process.join() end_time = time.perf_counter() - print('运行时间=', (end_time-start_time), '\n') + print('运行时间=', (end_time-start_time)) + # 合并数据 f = open('result.txt', 'w') for task_index in range(task_num): with open('task_index='+str(task_index)+'.txt', 'r') as f0: diff --git a/2020.06.01_parallel_calculations_with_python/parallel_calculations_with_python.py b/2020.06.01_parallel_calculations_with_python/parallel_calculations_with_python.py index 60b03fe..259e7c6 100644 --- a/2020.06.01_parallel_calculations_with_python/parallel_calculations_with_python.py +++ b/2020.06.01_parallel_calculations_with_python/parallel_calculations_with_python.py @@ -3,7 +3,7 @@ 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 """ -from multiprocessing import Process +import multiprocessing import os import time @@ -31,10 +31,10 @@ if __name__ == '__main__': print('并行程序') print('Process id = %s.' % os.getpid()) start_time = time.perf_counter() - p1 = Process(target=run_proc, args=('job1',)) - p2 = Process(target=run_proc, args=('job2',)) - p3 = Process(target=run_proc, args=('job3',)) - p4 = Process(target=run_proc, args=('job4',)) + p1 = multiprocessing.Process(target=run_proc, args=('job1',)) + p2 = multiprocessing.Process(target=run_proc, args=('job2',)) + p3 = multiprocessing.Process(target=run_proc, args=('job3',)) + p4 = multiprocessing.Process(target=run_proc, args=('job4',)) p1.start() p2.start() p3.start() diff --git a/2020.06.01_parallel_calculations_with_python/parallel_calculations_with_python_using_Value.py b/2020.06.01_parallel_calculations_with_python/parallel_calculations_with_python_using_Value.py index ee92b15..58b6641 100644 --- a/2020.06.01_parallel_calculations_with_python/parallel_calculations_with_python_using_Value.py +++ b/2020.06.01_parallel_calculations_with_python/parallel_calculations_with_python_using_Value.py @@ -3,16 +3,16 @@ 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 """ -from multiprocessing import Process, Value +import multiprocessing def run_proc(name, a, num): # 要执行的代码 num.value = a if __name__ == '__main__': - num1 = Value('d', 0.0) # 共享内存 - num2 = Value('d', 0.0) # 共享内存 - p1 = Process(target=run_proc, args=('job1', 100, num1)) - p2 = Process(target=run_proc, args=('job2', 200, num2)) + num1 = multiprocessing.Value('d', 0.0) # 共享内存 + num2 = multiprocessing.Value('d', 0.0) # 共享内存 + p1 = multiprocessing.Process(target=run_proc, args=('job1', 100, num1)) + p2 = multiprocessing.Process(target=run_proc, args=('job2', 200, num2)) p1.start() p2.start() p1.join() diff --git a/2020.06.01_parallel_calculations_with_python/parallel_calculations_with_python_using_kwargs.py b/2020.06.01_parallel_calculations_with_python/parallel_calculations_with_python_using_kwargs.py new file mode 100644 index 0000000..8406576 --- /dev/null +++ b/2020.06.01_parallel_calculations_with_python/parallel_calculations_with_python_using_kwargs.py @@ -0,0 +1,35 @@ +""" +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 +""" + +from multiprocessing import Process +import os +import time + +def run_proc(name, a=0, b=-1): # 要执行的代码 + start_time = time.perf_counter() + time.sleep(2) + end_time = time.perf_counter() + print ('Process id running on %s = %s' % (name, os.getpid()), f'; Values: a={a}, b={b}', '; running time = %s' % (end_time-start_time)) + + +if __name__ == '__main__': + + print('并行程序') + print('Process id = %s.' % os.getpid()) + start_time = time.perf_counter() + p1 = Process(target=run_proc, kwargs={'name':'job1', 'a':10, 'b':100}) + p2 = Process(target=run_proc, kwargs={'name':'job2', 'a':20}) + p3 = Process(target=run_proc, kwargs={'name':'job3', 'b':300}) + p4 = Process(target=run_proc, kwargs={'name':'job4'}) + p1.start() + p2.start() + p3.start() + p4.start() + p1.join() # join()方法可以等待子进程结束后再继续往下运行 + p2.join() + p3.join() + p4.join() + end_time = time.perf_counter() + print('运行时间(s)=', (end_time-start_time)) \ No newline at end of file