* 부모 프로세스, 자식 프로세스 관계 PID로 확인
1
2
3
4
5
6
7
8
9
10
11
12
|
from multiprocessing import Process
import os
def foo():
print('foo: child process: ', os.getpid())
print('foo: parent process: ', os.getppid())
if __name__ == '__main__':
print('parent process', os.getpid())
child1 = Process(target=foo).start()
child2 = Process(target=foo).start()
child3 = Process(target=foo).start()
|

* 멀티 프로세스 구현
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
from multiprocessing import Process
import os
# 기능 별 각기 다른 프로세스 생성
def aoo():
print('aoo executed!!') # 입력 기능
def boo():
print('boo executed!!') # 출력 기능
def coo():
print('coo executed!!') # 검사 기능
if __name__ == '__main__':
print('parent process', os.getpid())
child1 = Process(target=aoo).start()
child2 = Process(target=boo).start()
child3 = Process(target=coo).start()
|

* 동일한 프로세스에서 파생된 여러 스레드
1
2
3
4
5
6
7
8
9
10
11
12
|
import threading
import os
def foo():
print('foo: my thread id is: ', threading.get_native_id()) # thread id print
print('foo: my pid is: ', os.getpid()) # process id print
if __name__ == '__main__':
print('my pid is: ', os.getpid())
thread1 = threading.Thread(target=foo).start()
thread2 = threading.Thread(target=foo).start()
thread3 = threading.Thread(target=foo).start()
|

* 멀티 스레드 구현
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import threading
import os
def aoo():
print('aoo executed!!') # 입력 기능
def boo():
print('boo executed!!') # 출력 기능
def coo():
print('coo executed!!') # 검사 기능
if __name__ == '__main__':
print('my pid is: ', os.getpid())
thread1 = threading.Thread(target=aoo).start()
thread2 = threading.Thread(target=boo).start()
thread3 = threading.Thread(target=coo).start()
|

'PRACTICE > Basic' 카테고리의 다른 글
[C] 입출력장치 레지스터 조작 (인터럽트 서비스 루틴) (0) | 2025.03.14 |
---|---|
[JAVA] '좋은 수' 구하기 (백준 1253번) (0) | 2024.08.30 |
[JAVA] 구간 합 구하기 (백준 11659번) (0) | 2024.08.30 |
[Python] 패스워드 생성기 (0) | 2021.03.02 |
[Python] 문자열 역순 출력 - reverse_sentence (0) | 2021.03.02 |
댓글