From 14bee41b70672d84b1ef9bb7cfca4ce5e60f43e6 Mon Sep 17 00:00:00 2001 From: guanjihuan Date: Tue, 22 Feb 2022 14:34:21 +0800 Subject: [PATCH] update --- ...on_of_parallel_calculations_with_python.py | 10 +- .../parallel_calculations_with_python.py | 21 ++-- ...el_calculations_with_python_using_Value.py | 16 +++ .../results/a0.txt | 16 --- .../results/a1.txt | 16 --- .../results/a2.txt | 16 --- .../results/a3.txt | 16 --- .../results/a4.txt | 16 --- .../results/a5.txt | 16 --- .../results/a6.txt | 4 - .../results/combine.txt | 100 ------------------ 11 files changed, 35 insertions(+), 212 deletions(-) create mode 100644 language_learning/2020.06.01_parallel_calculations_with_python/parallel_calculations_with_python_using_Value.py delete mode 100755 language_learning/2021.05.10_parallel_calculations_using_sh_files/results/a0.txt delete mode 100755 language_learning/2021.05.10_parallel_calculations_using_sh_files/results/a1.txt delete mode 100755 language_learning/2021.05.10_parallel_calculations_using_sh_files/results/a2.txt delete mode 100755 language_learning/2021.05.10_parallel_calculations_using_sh_files/results/a3.txt delete mode 100755 language_learning/2021.05.10_parallel_calculations_using_sh_files/results/a4.txt delete mode 100755 language_learning/2021.05.10_parallel_calculations_using_sh_files/results/a5.txt delete mode 100755 language_learning/2021.05.10_parallel_calculations_using_sh_files/results/a6.txt delete mode 100755 language_learning/2021.05.10_parallel_calculations_using_sh_files/results/combine.txt diff --git a/language_learning/2020.06.01_parallel_calculations_with_python/application_of_parallel_calculations_with_python.py b/language_learning/2020.06.01_parallel_calculations_with_python/application_of_parallel_calculations_with_python.py index 6b49a5d..2267f25 100644 --- a/language_learning/2020.06.01_parallel_calculations_with_python/application_of_parallel_calculations_with_python.py +++ b/language_learning/2020.06.01_parallel_calculations_with_python/application_of_parallel_calculations_with_python.py @@ -10,17 +10,21 @@ def main(parameter_array, task_index): for parameter in parameter_array: result = parameter*2 result_array.append(result) + time.sleep(np.random.uniform(1,10)) guan.write_one_dimensional_data(parameter_array, result_array, filename='task_index='+str(task_index)) if __name__ == '__main__': cpus = 4 parameter_array_all = np.arange(0, 17, 1) start_time = time.perf_counter() + process_array = [] for task_index in range(cpus): parameter_array = guan.preprocess_for_parallel_calculations(parameter_array_all, cpus, task_index) - p = Process(target=main, args=(parameter_array, task_index)) - p.start() - p.join() + process_array.append(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') f = open('result.txt', 'w') diff --git a/language_learning/2020.06.01_parallel_calculations_with_python/parallel_calculations_with_python.py b/language_learning/2020.06.01_parallel_calculations_with_python/parallel_calculations_with_python.py index 35e20cf..cc41a57 100755 --- a/language_learning/2020.06.01_parallel_calculations_with_python/parallel_calculations_with_python.py +++ b/language_learning/2020.06.01_parallel_calculations_with_python/parallel_calculations_with_python.py @@ -26,14 +26,17 @@ if __name__ == '__main__': print('并行程序') print('Process id = %s.' % os.getpid()) start_time = time.perf_counter() - p = Process(target=run_proc, args=('job1',)) - p.start() - p = Process(target=run_proc, args=('job2',)) - p.start() - p = Process(target=run_proc, args=('job3',)) - p.start() - p = Process(target=run_proc, args=('job4',)) - p.start() - p.join() # join()方法可以等待子进程结束后再继续往下运行 + 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.start() + p2.start() + p3.start() + p4.start() + p1.join() # join()方法可以等待子进程结束后再继续往下运行 + p2.join() # join()方法可以等待子进程结束后再继续往下运行 + p3.join() # join()方法可以等待子进程结束后再继续往下运行 + p4.join() # join()方法可以等待子进程结束后再继续往下运行 end_time = time.perf_counter() print('运行时间(s)=', (end_time-start_time)) \ No newline at end of file diff --git a/language_learning/2020.06.01_parallel_calculations_with_python/parallel_calculations_with_python_using_Value.py b/language_learning/2020.06.01_parallel_calculations_with_python/parallel_calculations_with_python_using_Value.py new file mode 100644 index 0000000..be08ebe --- /dev/null +++ b/language_learning/2020.06.01_parallel_calculations_with_python/parallel_calculations_with_python_using_Value.py @@ -0,0 +1,16 @@ +from multiprocessing import Process, Value + +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)) + p1.start() + p2.start() + p1.join() + p2.join() + print(num1.value) + print(num2.value) \ No newline at end of file diff --git a/language_learning/2021.05.10_parallel_calculations_using_sh_files/results/a0.txt b/language_learning/2021.05.10_parallel_calculations_using_sh_files/results/a0.txt deleted file mode 100755 index 510d166..0000000 --- a/language_learning/2021.05.10_parallel_calculations_using_sh_files/results/a0.txt +++ /dev/null @@ -1,16 +0,0 @@ -0.0 0.0 -0.1 0.010000000000000002 -0.2 0.04000000000000001 -0.30000000000000004 0.09000000000000002 -0.4 0.16000000000000003 -0.5 0.25 -0.6000000000000001 0.3600000000000001 -0.7000000000000001 0.4900000000000001 -0.8 0.6400000000000001 -0.9 0.81 -1.0 1.0 -1.1 1.2100000000000002 -1.2000000000000002 1.4400000000000004 -1.3 1.6900000000000002 -1.4000000000000001 1.9600000000000004 -1.5 2.25 diff --git a/language_learning/2021.05.10_parallel_calculations_using_sh_files/results/a1.txt b/language_learning/2021.05.10_parallel_calculations_using_sh_files/results/a1.txt deleted file mode 100755 index 79e3156..0000000 --- a/language_learning/2021.05.10_parallel_calculations_using_sh_files/results/a1.txt +++ /dev/null @@ -1,16 +0,0 @@ -1.6 2.5600000000000005 -1.7000000000000002 2.8900000000000006 -1.8 3.24 -1.9000000000000001 3.6100000000000003 -2.0 4.0 -2.1 4.41 -2.2 4.840000000000001 -2.3000000000000003 5.290000000000001 -2.4000000000000004 5.760000000000002 -2.5 6.25 -2.6 6.760000000000001 -2.7 7.290000000000001 -2.8000000000000003 7.840000000000002 -2.9000000000000004 8.410000000000002 -3.0 9.0 -3.1 9.610000000000001 diff --git a/language_learning/2021.05.10_parallel_calculations_using_sh_files/results/a2.txt b/language_learning/2021.05.10_parallel_calculations_using_sh_files/results/a2.txt deleted file mode 100755 index c312024..0000000 --- a/language_learning/2021.05.10_parallel_calculations_using_sh_files/results/a2.txt +++ /dev/null @@ -1,16 +0,0 @@ -3.2 10.240000000000002 -3.3000000000000003 10.890000000000002 -3.4000000000000004 11.560000000000002 -3.5 12.25 -3.6 12.96 -3.7 13.690000000000001 -3.8000000000000003 14.440000000000001 -3.9000000000000004 15.210000000000003 -4.0 16.0 -4.1000000000000005 16.810000000000006 -4.2 17.64 -4.3 18.49 -4.4 19.360000000000003 -4.5 20.25 -4.6000000000000005 21.160000000000004 -4.7 22.090000000000003 diff --git a/language_learning/2021.05.10_parallel_calculations_using_sh_files/results/a3.txt b/language_learning/2021.05.10_parallel_calculations_using_sh_files/results/a3.txt deleted file mode 100755 index 6aa79d3..0000000 --- a/language_learning/2021.05.10_parallel_calculations_using_sh_files/results/a3.txt +++ /dev/null @@ -1,16 +0,0 @@ -4.800000000000001 23.040000000000006 -4.9 24.010000000000005 -5.0 25.0 -5.1000000000000005 26.010000000000005 -5.2 27.040000000000003 -5.300000000000001 28.090000000000007 -5.4 29.160000000000004 -5.5 30.25 -5.6000000000000005 31.360000000000007 -5.7 32.49 -5.800000000000001 33.64000000000001 -5.9 34.81 -6.0 36.0 -6.1000000000000005 37.21000000000001 -6.2 38.440000000000005 -6.300000000000001 39.69000000000001 diff --git a/language_learning/2021.05.10_parallel_calculations_using_sh_files/results/a4.txt b/language_learning/2021.05.10_parallel_calculations_using_sh_files/results/a4.txt deleted file mode 100755 index 14b3470..0000000 --- a/language_learning/2021.05.10_parallel_calculations_using_sh_files/results/a4.txt +++ /dev/null @@ -1,16 +0,0 @@ -6.4 40.96000000000001 -6.5 42.25 -6.6000000000000005 43.56000000000001 -6.7 44.89 -6.800000000000001 46.24000000000001 -6.9 47.61000000000001 -7.0 49.0 -7.1000000000000005 50.41000000000001 -7.2 51.84 -7.300000000000001 53.29000000000001 -7.4 54.760000000000005 -7.5 56.25 -7.6000000000000005 57.760000000000005 -7.7 59.290000000000006 -7.800000000000001 60.84000000000001 -7.9 62.410000000000004 diff --git a/language_learning/2021.05.10_parallel_calculations_using_sh_files/results/a5.txt b/language_learning/2021.05.10_parallel_calculations_using_sh_files/results/a5.txt deleted file mode 100755 index 5953650..0000000 --- a/language_learning/2021.05.10_parallel_calculations_using_sh_files/results/a5.txt +++ /dev/null @@ -1,16 +0,0 @@ -8.0 64.0 -8.1 65.61 -8.200000000000001 67.24000000000002 -8.3 68.89000000000001 -8.4 70.56 -8.5 72.25 -8.6 73.96 -8.700000000000001 75.69000000000001 -8.8 77.44000000000001 -8.9 79.21000000000001 -9.0 81.0 -9.1 82.80999999999999 -9.200000000000001 84.64000000000001 -9.3 86.49000000000001 -9.4 88.36000000000001 -9.5 90.25 diff --git a/language_learning/2021.05.10_parallel_calculations_using_sh_files/results/a6.txt b/language_learning/2021.05.10_parallel_calculations_using_sh_files/results/a6.txt deleted file mode 100755 index f674c18..0000000 --- a/language_learning/2021.05.10_parallel_calculations_using_sh_files/results/a6.txt +++ /dev/null @@ -1,4 +0,0 @@ -9.600000000000001 92.16000000000003 -9.700000000000001 94.09000000000002 -9.8 96.04000000000002 -9.9 98.01 diff --git a/language_learning/2021.05.10_parallel_calculations_using_sh_files/results/combine.txt b/language_learning/2021.05.10_parallel_calculations_using_sh_files/results/combine.txt deleted file mode 100755 index a686e5e..0000000 --- a/language_learning/2021.05.10_parallel_calculations_using_sh_files/results/combine.txt +++ /dev/null @@ -1,100 +0,0 @@ -0.0 0.0 -0.1 0.010000000000000002 -0.2 0.04000000000000001 -0.30000000000000004 0.09000000000000002 -0.4 0.16000000000000003 -0.5 0.25 -0.6000000000000001 0.3600000000000001 -0.7000000000000001 0.4900000000000001 -0.8 0.6400000000000001 -0.9 0.81 -1.0 1.0 -1.1 1.2100000000000002 -1.2000000000000002 1.4400000000000004 -1.3 1.6900000000000002 -1.4000000000000001 1.9600000000000004 -1.5 2.25 -1.6 2.5600000000000005 -1.7000000000000002 2.8900000000000006 -1.8 3.24 -1.9000000000000001 3.6100000000000003 -2.0 4.0 -2.1 4.41 -2.2 4.840000000000001 -2.3000000000000003 5.290000000000001 -2.4000000000000004 5.760000000000002 -2.5 6.25 -2.6 6.760000000000001 -2.7 7.290000000000001 -2.8000000000000003 7.840000000000002 -2.9000000000000004 8.410000000000002 -3.0 9.0 -3.1 9.610000000000001 -3.2 10.240000000000002 -3.3000000000000003 10.890000000000002 -3.4000000000000004 11.560000000000002 -3.5 12.25 -3.6 12.96 -3.7 13.690000000000001 -3.8000000000000003 14.440000000000001 -3.9000000000000004 15.210000000000003 -4.0 16.0 -4.1000000000000005 16.810000000000006 -4.2 17.64 -4.3 18.49 -4.4 19.360000000000003 -4.5 20.25 -4.6000000000000005 21.160000000000004 -4.7 22.090000000000003 -4.800000000000001 23.040000000000006 -4.9 24.010000000000005 -5.0 25.0 -5.1000000000000005 26.010000000000005 -5.2 27.040000000000003 -5.300000000000001 28.090000000000007 -5.4 29.160000000000004 -5.5 30.25 -5.6000000000000005 31.360000000000007 -5.7 32.49 -5.800000000000001 33.64000000000001 -5.9 34.81 -6.0 36.0 -6.1000000000000005 37.21000000000001 -6.2 38.440000000000005 -6.300000000000001 39.69000000000001 -6.4 40.96000000000001 -6.5 42.25 -6.6000000000000005 43.56000000000001 -6.7 44.89 -6.800000000000001 46.24000000000001 -6.9 47.61000000000001 -7.0 49.0 -7.1000000000000005 50.41000000000001 -7.2 51.84 -7.300000000000001 53.29000000000001 -7.4 54.760000000000005 -7.5 56.25 -7.6000000000000005 57.760000000000005 -7.7 59.290000000000006 -7.800000000000001 60.84000000000001 -7.9 62.410000000000004 -8.0 64.0 -8.1 65.61 -8.200000000000001 67.24000000000002 -8.3 68.89000000000001 -8.4 70.56 -8.5 72.25 -8.6 73.96 -8.700000000000001 75.69000000000001 -8.8 77.44000000000001 -8.9 79.21000000000001 -9.0 81.0 -9.1 82.80999999999999 -9.200000000000001 84.64000000000001 -9.3 86.49000000000001 -9.4 88.36000000000001 -9.5 90.25 -9.600000000000001 92.16000000000003 -9.700000000000001 94.09000000000002 -9.8 96.04000000000002 -9.9 98.01