Skip to content

Commit 2c726ba

Browse files
committed
Multiprocessing\S05-terminate-kill-exitcode\S05-01-terminate-kill-exitcode
1 parent b3e86f4 commit 2c726ba

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from multiprocessing import Process
2+
import time
3+
4+
5+
start = time.perf_counter()
6+
7+
def some_task(name):
8+
print(f"Process {name} Started.")
9+
time.sleep(3)
10+
print(f"Process {name} Finished.")
11+
raise Exception("Oops!!!")
12+
13+
14+
if __name__ == "__main__":
15+
p1 = Process(target=some_task, args=("One", ), daemon=True)
16+
p2 = Process(target=some_task, args=("Two", ), daemon=True)
17+
18+
p1.start()
19+
p2.start()
20+
21+
print(p1.is_alive())
22+
print(p2.is_alive())
23+
24+
p1.terminate() # SIGTERM
25+
p2.kill() # SIGKILL
26+
27+
p1.join()
28+
p2.join()
29+
30+
print(p1.is_alive())
31+
print(p2.is_alive())
32+
33+
print(p1.exitcode)
34+
print(p2.exitcode)
35+
36+
end = time.perf_counter()
37+
exe_time = end - start
38+
print(f"Execution Time: {exe_time} sec")
39+
40+
41+
# terminate()
42+
# kill()
43+
# exitcode = {
44+
# "0": "Exit Without Any Erorr",
45+
# ">0": "Exit for Some Erorr",
46+
# "<0": "Exit By Signal (terminate or kill)",
47+
# }

0 commit comments

Comments
 (0)