-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathio_mp_python.py
More file actions
67 lines (46 loc) · 1.68 KB
/
io_mp_python.py
File metadata and controls
67 lines (46 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import ray
import time
import os
import cv2
from multiprocessing import Process, cpu_count, Manager
total_iters = 10000
image_path = './input.jpg'
output_path = "./outputs"
if not os.path.exists(output_path):
os.makedirs(output_path)
#########################################################
# Parallel Execution of test function #
#########################################################
start_time = time.time()
# max 1000 processes can be called
max_iters = min(2000, total_iters)
processes = []
def test_function_parallel(ii, image_path):
img = cv2.imread(image_path)
cv2.imwrite(os.path.join(output_path, str(ii) + ".jpg"), img)
return
start = time.time()
for ii in range(total_iters):
pcs = Process(target = test_function_parallel, args=(ii, image_path))
processes.append(pcs)
pcs.start()
if ii % max_iters == 0 or ii == len(range(total_iters)) - 1:
for jj in range(len(processes)):
processes[jj].join()
processes = []
for jj in range(len(processes)):
processes[jj].join()
print("Time taken by Parallel Execution in seconds : ", time.time() - start)
# #########################################################
# # Serial Execution of test function #
# #########################################################
# def test_function_serial(ii, image_path):
# img = cv2.imread(image_path)
# cv2.imwrite(os.path.join(output_path, str(ii) + ".jpg"), img)
# return
# a = 1
# b = 1
# start = time.time()
# for ii in range(total_iters):
# test_function_serial(ii, image_path)
# print("Time taken by Serial Execution in seconds : ", time.time() - start)