-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpy_ad_2_3.py
More file actions
53 lines (40 loc) · 1.11 KB
/
py_ad_2_3.py
File metadata and controls
53 lines (40 loc) · 1.11 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
"""
Section 2
Parallelism with Multiprocessing - multiprocessing(2) - Naming
Keyword - Naming, parallel processing
"""
from multiprocessing import Process, current_process
import os
import random
import time
# 실행 방법
def square(n):
# 랜덤 sleep
time.sleep(random.randint(1, 3))
process_id = os.getpid()
process_name = current_process().name
# 제곱
result = n * n
# 정보 출력
print(f"Process ID: {process_id}, Process Name: {process_name}")
print(f"Result of {n} square : {result}")
if __name__ == "__main__":
# 부모 프로세스 아이디
parent_process_id = os.getpid()
# 출력
print(f"Parent process ID {parent_process_id}")
# 프로세스 리스트 선언
processes = list()
# 프로세스 생성 및 실행
for i in range(1, 10): # 1 ~ 100 적절히 조절
# 생성
t = Process(name=str(i), target=square, args=(i,))
# 배열에 담기
processes.append(t)
# 시작
t.start()
# Join
for process in processes:
process.join()
# 종료
print("Main-Processing Done!")