-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
83 lines (60 loc) · 1.71 KB
/
test.py
File metadata and controls
83 lines (60 loc) · 1.71 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
from multiprocessing import Process, Queue, current_process
import time
import os
# 실행 함수
def worker(id, baseNum, q):
process_id = os.getpid()
process_name = current_process().name
print(f"Process ID: {process_id}, Process Name: {process_name}")
# 누적
sub_total = 0
# 계산
for i in range(baseNum):
sub_total += 1
print(sub_total)
# time.sleep(0.1)
# Produce
q.put(sub_total)
# 정보 출력
print(f"*** Sub Result : {sub_total},")
def main():
# 부모 프로세스 아이디
parent_process_id = os.getpid()
# 출력
print(f"Parent process ID {parent_process_id}")
# 프로세스 리스트 선언
processes = list()
# 시작 시간
start_time = time.time()
# Queue 선언
q = Queue()
# 프로세스 생성 및 실행
for i in range(4): # 1 ~ 100 적절히 조절
# 생성
t = Process(name=str(i).zfill(2), target=worker, args=(i, 100000000, q))
# 배열에 담기
processes.append(t)
# 시작
t.start()
# Join
for process in processes:
process.join()
# 순수 계산 시간
print("--- %s seconds ---" % (time.time() - start_time))
# 종료 플래그
q.put('exit')
total = 0
# 대기
while True:
tmp = q.get()
if tmp == 'exit':
break
else:
total += tmp
print()
print("Main-Processing Total_count={}".format(total))
print("Main-Processing Done!")
# 마찬가지로 Pipe 사용법은 링크 별도 첨부 예제 참조
# https://docs.python.org/3/library/multiprocessing.html#exchanging-objects-between-processes
if __name__ == "__main__":
main()