category
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
from multiprocessing import Process
|
||||
import os
|
||||
import time
|
||||
import numpy as np
|
||||
import guan
|
||||
|
||||
def main(parameter_array, task_index):
|
||||
print ('Process id = %s' % (os.getpid()))
|
||||
result_array = []
|
||||
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)
|
||||
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')
|
||||
for task_index in range(cpus):
|
||||
with open('task_index='+str(task_index)+'.txt', 'r') as f0:
|
||||
text = f0.read()
|
||||
f.write(text)
|
||||
f.close()
|
@@ -0,0 +1,42 @@
|
||||
from multiprocessing import Process
|
||||
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__':
|
||||
|
||||
# 串行
|
||||
print('串行程序')
|
||||
print('Process id = %s.' % os.getpid())
|
||||
start_time = time.perf_counter()
|
||||
run_proc('job1')
|
||||
run_proc('job2')
|
||||
run_proc('job3')
|
||||
run_proc('job4')
|
||||
end_time = time.perf_counter()
|
||||
print('CPU执行时间(s)=', (end_time-start_time), '\n')
|
||||
|
||||
# 并行
|
||||
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.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))
|
@@ -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)
|
Reference in New Issue
Block a user