Skip to content

Commit 1cf1f53

Browse files
committed
Multiprocessing\S01-Process\S01-02-Process-multiprocessing-first-example
1 parent 37f5887 commit 1cf1f53

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import multiprocessing
2+
import time
3+
4+
def some_task(name):
5+
print(f"Process {name} Started.")
6+
for i in range(0, 100000000):
7+
pass
8+
print(f"Process {name} Finished.")
9+
10+
if __name__ == "__main__":
11+
print(multiprocessing.cpu_count())
12+
13+
start = time.perf_counter()
14+
p1 = multiprocessing.Process(target=some_task, args=("One", ))
15+
p2 = multiprocessing.Process(target=some_task, args=("Two", ))
16+
p3 = multiprocessing.Process(target=some_task, args=("Three", ))
17+
p4 = multiprocessing.Process(target=some_task, args=("Four", ))
18+
19+
# start Processes
20+
p1.start()
21+
p2.start()
22+
p3.start()
23+
p4.start()
24+
25+
# join Processes
26+
p1.join()
27+
p2.join()
28+
p3.join()
29+
p4.join()
30+
31+
end = time.perf_counter()
32+
exe_time = end - start
33+
print(f"Execution Time: {exe_time} sec")
34+
35+
# for example: Execution Time: 6.819241099990904 sec

0 commit comments

Comments
 (0)