update
This commit is contained in:
parent
85f2ba5768
commit
67c535567a
@ -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
|
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 os
|
||||||
import time
|
import time
|
||||||
import numpy as np
|
import numpy as np
|
||||||
@ -25,13 +25,14 @@ if __name__ == '__main__':
|
|||||||
process_array = []
|
process_array = []
|
||||||
for task_index in range(task_num):
|
for task_index in range(task_num):
|
||||||
parameter_array = guan.preprocess_for_parallel_calculations(parameter_array_all, task_num, task_index)
|
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: # 运行子进程
|
for process in process_array: # 运行子进程
|
||||||
process.start()
|
process.start()
|
||||||
for process in process_array: # 等待子进程完成
|
for process in process_array: # 等待子进程完成
|
||||||
process.join()
|
process.join()
|
||||||
end_time = time.perf_counter()
|
end_time = time.perf_counter()
|
||||||
print('运行时间=', (end_time-start_time), '\n')
|
print('运行时间=', (end_time-start_time))
|
||||||
|
# 合并数据
|
||||||
f = open('result.txt', 'w')
|
f = open('result.txt', 'w')
|
||||||
for task_index in range(task_num):
|
for task_index in range(task_num):
|
||||||
with open('task_index='+str(task_index)+'.txt', 'r') as f0:
|
with open('task_index='+str(task_index)+'.txt', 'r') as f0:
|
||||||
|
@ -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
|
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 os
|
||||||
import time
|
import time
|
||||||
|
|
||||||
@ -31,10 +31,10 @@ if __name__ == '__main__':
|
|||||||
print('并行程序')
|
print('并行程序')
|
||||||
print('Process id = %s.' % os.getpid())
|
print('Process id = %s.' % os.getpid())
|
||||||
start_time = time.perf_counter()
|
start_time = time.perf_counter()
|
||||||
p1 = Process(target=run_proc, args=('job1',))
|
p1 = multiprocessing.Process(target=run_proc, args=('job1',))
|
||||||
p2 = Process(target=run_proc, args=('job2',))
|
p2 = multiprocessing.Process(target=run_proc, args=('job2',))
|
||||||
p3 = Process(target=run_proc, args=('job3',))
|
p3 = multiprocessing.Process(target=run_proc, args=('job3',))
|
||||||
p4 = Process(target=run_proc, args=('job4',))
|
p4 = multiprocessing.Process(target=run_proc, args=('job4',))
|
||||||
p1.start()
|
p1.start()
|
||||||
p2.start()
|
p2.start()
|
||||||
p3.start()
|
p3.start()
|
||||||
|
@ -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
|
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): # 要执行的代码
|
def run_proc(name, a, num): # 要执行的代码
|
||||||
num.value = a
|
num.value = a
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
num1 = Value('d', 0.0) # 共享内存
|
num1 = multiprocessing.Value('d', 0.0) # 共享内存
|
||||||
num2 = Value('d', 0.0) # 共享内存
|
num2 = multiprocessing.Value('d', 0.0) # 共享内存
|
||||||
p1 = Process(target=run_proc, args=('job1', 100, num1))
|
p1 = multiprocessing.Process(target=run_proc, args=('job1', 100, num1))
|
||||||
p2 = Process(target=run_proc, args=('job2', 200, num2))
|
p2 = multiprocessing.Process(target=run_proc, args=('job2', 200, num2))
|
||||||
p1.start()
|
p1.start()
|
||||||
p2.start()
|
p2.start()
|
||||||
p1.join()
|
p1.join()
|
||||||
|
@ -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))
|
Loading…
x
Reference in New Issue
Block a user