-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest43_multiprocessing.py
More file actions
35 lines (27 loc) · 1.13 KB
/
test43_multiprocessing.py
File metadata and controls
35 lines (27 loc) · 1.13 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
# new way of doing multiprocessing
import concurrent.futures
import time
start = time.perf_counter()
def do_something(seconds):
print(f'Sleeping for {seconds} second(s)...')
time.sleep(seconds)
return f'Done Sleeping...{seconds}'
if __name__ == '__main__':
# the context manager below automatically joins the processes
with concurrent.futures.ProcessPoolExecutor() as executor:
# f1 = executor.submit(do_something, 1)
# f2 = executor.submit(do_something, 1)
# print(f1.result())
# print(f1.result())
secs = [5,4,3,2,1]
#results = [executor.submit(do_something, 1) for _ in range(10)]
#results = [executor.submit(do_something, sec) for sec in secs]
results = executor.map(do_something, secs)
# prints the results in the order that they completed
# for f in concurrent.futures.as_completed(results):
# print(f.result())
# map returns results in the order that they were started
for result in results:
print(result)
finish = time.perf_counter()
print(f'Finished in {round(finish-start,2)} second(s)')