-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial_9.py
More file actions
389 lines (284 loc) · 11.6 KB
/
tutorial_9.py
File metadata and controls
389 lines (284 loc) · 11.6 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# Python Multiprocessing Tutorial: Run Code in Parallel Using the Multiprocessing Module
import time
def do_something():
print("Sleeping for 1 second...")
time.sleep(1)
print("Done Sleeping for 1 second...")
return ""
# do_something2 requires the seconds parameter to run
def do_something2(seconds):
print(f"Sleeping for {seconds} second...")
time.sleep(seconds)
print(f"Done Sleeping for {seconds} second...")
return ""
# do_something2 requires the seconds parameter to run and returns a value
def do_something3(seconds):
print(f"Sleeping for {seconds} second...")
time.sleep(seconds)
return f"Done Sleeping for {seconds} second..."
def original_function():
# Original function
start = time.perf_counter()
# Starting a counter to know the execution time of the script
# Running in order like this, is called running synchronously as it is running one after the other
print("Synchronous Code")
print()
for _ in range(2):
# _ variable is a throw away variable in python, which has no actual use
# print(do_something3(1.3))
print(do_something())
finish = time.perf_counter()
print(f"Finished in {round(finish-start, 2)} second(s)")
print()
print()
# Difference between CPU bound and IO bound task
# CPU bound is when CPU makes delays to process something and we have to wait
# IO bound, same as CPU bound but here we wait for input and output operations to be completed
# example of IO bound task: reading and writing to the file system, network operations, downloading files online
# CPU bound task do not benefit from using threading
# Some program even run slow using threads because of the added overhead cost for creating and destroying different threads
# CPU bound task use multiprocessing and run the jobs in parallel instead
# How multiprocessing works?
# The code is spread out to run on multiple processors on the machine
# And the tasks are running at the same time in parallel on multiple processors unlike threading
# When to use threading or multiprocessing?
# It depends on what are task are we performing and the computer's hardware,
# Or if the task is a CPU bound task(multiprocessing) or IO bound task(threading)
import multiprocessing
# Manual Multiprocessing Method - the old way
def example_1():
start = time.perf_counter()
print("Manual Multiprocessing Method")
print()
p1 = multiprocessing.Process(target=do_something)
p2 = multiprocessing.Process(target=do_something)
# Start the processes
p1.start()
p2.start()
# To tell the process that they have to finish before executing the rest of the code as they are dependant on the process completion
# Output when not using process.join()
# Process take longer to spin up than threads
"""
Manual Multiprocessing Method
Finished in 0.01 second(s)
Sleeping for 1 second...
Sleeping for 1 second...
Done Sleeping for 1 second...
Done Sleeping for 1 second...
"""
p1.join()
p2.join()
finish = time.perf_counter()
print(f"Finished in {round(finish-start, 2)} second(s)")
print()
print()
def example_2():
start = time.perf_counter()
print("Multiple parallel jobs using Manual Multiprocessing Method")
print()
# Keep track of all processes running
processes = []
for _ in range(10):
# Even though the computer does not have 10 cores,
# The CPU has ways of switching between cores when one of them is not too busy
p = multiprocessing.Process(target=do_something)
p.start()
processes.append(p)
# Cannot run process.join() inside the loop as it will become same as synchronous code
# End all process execution before continuing
for process in processes:
process.join()
finish = time.perf_counter()
print(f"Finished in {round(finish-start, 2)} second(s)")
print()
print()
def example_3():
start = time.perf_counter()
print("Multiple parallel jobs using Manual Multiprocessing Method")
print("Using functions with arguments without return")
print()
# Keep track of all processes running
processes = []
for _ in range(10):
# Even though the computer does not have 10 cores,
# The CPU has ways of switching between cores when one of them is not too busy
# For multiprocessing, the arguments must be able to be serialised using pickle
# Serialising using pickle means we are converting python objects into a format that can be deconstructed and reconstructed in another python script
p = multiprocessing.Process(target=do_something2, args=[1.5])
p.start()
processes.append(p)
# Cannot run process.join() inside the loop as it will become same as synchronous code
# End all process execution before continuing
for process in processes:
process.join()
finish = time.perf_counter()
print(f"Finished in {round(finish-start, 2)} second(s)")
print()
print()
# Code below will not work on windows and it will crash in windows CMD
# It crashes in vscode python debugger, I could not figure out why though
# It works fine in linux terminal
import queue
# def helper(queue, seconds):
# queue.put(do_something3(seconds))
# def display_que(que):
# while not que.empty():
# result = que.get()
# print(result)
def example_4():
start = time.perf_counter()
print("Multiple parallel jobs using Manual Multiprocessing Method")
print("Using functions with arguments with return")
print()
# Queue for helper function
que = queue.Queue()
# Keep track of all processes running
processes = []
for _ in range(10):
# Attempt of using same lambda expression logic as in the Threading tutorial (tutorial_8)
# Attempt fails as there are some limitation in Python regarding pickling lambda expressions in windows
# It works on linux
p = multiprocessing.Process(
target=lambda queue, seconds: queue.put(do_something3(seconds)), args=[que, 1.5]
)
# How to resolve the issue in windows?
# Create a helper function which will do the same job that the lambda expression was doing
# p = multiprocessing.Process(target=helper, args=[que, 1.5])
p.start()
processes.append(p)
# Cannot run process.join() inside the loop as it will become same as synchronous code
# End all process execution before continuing
for process in processes:
process.join()
# Check thread's return value
while not que.empty():
result = que.get()
print(result)
# display_que(que)
# I could not get it to display the return of the function do_something3
"""
Multiple parallel jobs using Manual Multiprocessing Method
Using functions with arguments with return
Sleeping for 1.5 second...
Sleeping for 1.5 second...
Sleeping for 1.5 second...
Sleeping for 1.5 second...
Sleeping for 1.5 second...
Sleeping for 1.5 second...
Sleeping for 1.5 second...
Sleeping for 1.5 second...
Sleeping for 1.5 second...
Sleeping for 1.5 second...
Finished in 1.51 second(s)
"""
finish = time.perf_counter()
print(f"Finished in {round(finish-start, 2)} second(s)")
print()
print()
import concurrent.futures
def example_5():
start = time.perf_counter()
print("ProcessPool Executor Method")
print()
with concurrent.futures.ProcessPoolExecutor() as executor:
# Below is same as in Threading tutorial (tutorial_8)
# Submit method schedule a function to be executed and returns a future object
# Future object encapsulated the execution of the function and allows us to check in on it after it's been scheduled
# Can check if it is running, or if it's done, or the result
f1 = executor.submit(do_something3, 1.5)
f2 = executor.submit(do_something3, 1.5)
# Result method will wait for the function to complete and will get the return value of the function in submit
print(f1.result())
print(f2.result())
finish = time.perf_counter()
print(f"Finished in {round(finish-start, 2)} second(s)")
print()
print()
def example_6():
start = time.perf_counter()
print("Multiple parallel jobs using ProcessPool Executor Method")
print("Example 1")
print()
with concurrent.futures.ProcessPoolExecutor() as executor:
# List comprehension
results = [executor.submit(do_something3, 1.5) for _ in range(10)]
# as_completed will display the return value of do_something function in real time as it completes a parallel job
for f in concurrent.futures.as_completed(results):
print(f.result())
finish = time.perf_counter()
print(f"Finished in {round(finish-start, 2)} second(s)")
print()
print()
def example_7():
start = time.perf_counter()
print("Multiple parallel jobs using ProcessPool Executor Method")
print("Example 2")
print()
with concurrent.futures.ProcessPoolExecutor() as executor:
secs = [3, 2.5, 1.5, 1]
results = [executor.submit(do_something3, sec) for sec in secs]
# as_completed will display the return value of do_something function in real time as it completes a parallel job
for f in concurrent.futures.as_completed(results):
# The 1 second job will finish first then the 1.5, 2.5, 3 thus proving how as_completed works
# as_completed will return the future object in the order that they completed
print(f.result())
"""
Sleeping for 3 second...
Sleeping for 2.5 second...
Sleeping for 1.5 second...
Sleeping for 1 second...
Done Sleeping for 1 second...
Done Sleeping for 1.5 second...
Done Sleeping for 2.5 second...
Done Sleeping for 3 second...
Finished in 3.02 second(s)
"""
finish = time.perf_counter()
print(f"Finished in {round(finish-start, 2)} second(s)")
print()
print()
def example_8():
start = time.perf_counter()
print("Multiple parallel jobs using ProcessPool Executor Method")
print("Example 3")
print()
with concurrent.futures.ProcessPoolExecutor() as executor:
secs = [3, 2.5, 1.5, 1]
# To return the results in the same order that they started, we use the map function
# But they all ran concurrently and did not slow down
results = executor.map(do_something3, secs)
for result in results:
print(result)
"""
Sleeping for 3 second...
Sleeping for 2.5 second...
Sleeping for 1.5 second...
Sleeping for 1 second...
Done Sleeping for 3 second...
Done Sleeping for 2.5 second...
Done Sleeping for 1.5 second...
Done Sleeping for 1 second...
Finished in 3.02 second(s)
"""
finish = time.perf_counter()
print(f"Finished in {round(finish-start, 2)} second(s)")
print()
print()
def example_9():
pass
# the main function will call all other functions so that they will be executed once
def main():
original_function()
example_1()
example_2()
example_3()
example_4()
example_5()
example_6()
example_7()
example_8()
example_9()
# This needs to be added as a stopping condition on windows or else the program goes on a recursive loop till it crashes
# On linux it will work fine and will not require the function main() or this stopping condition
if __name__ == "__main__":
main()