T.I.L Apr 18, 2023 (process)
2023. 4. 18. 23:24ㆍT.I.L (Today_I_Learned)
Python으로 process 다루기 연습
process 만들기
code
print('hello, os')
result
hello, os
실행된 순간 문자열을 출력하는 process가 생성됩니다.
process PID 확인하기
code
import os
print('hello, os')
print('ijh\'s pid is', os.getpid())
result
hello, os
ijh's pid is 28016
PID 값은 운영체제가 그때그때 부여하는 값이기 때문에 실행 할 때마다 다른 PID값이 출력됩니다.
여러(자식) process 생성
code
from multiprocessing import Process
import os
def ijh():
print('child process', os.getpid())
print('my parent is', os.getppid())
if __name__ == '__main__':
print('parent process', os.getpid())
child = Process(target=ijh).start()
result
parent process 3472
child process 9984
my parent is 3472
- print('parent process', os.getpid()) code 는 ‘parent process + PID값’ 형태로 출력하는 코드
- child = Process(target=ijh).start() code 는 ijh 라는 함수를 실행하는 자식 프로세스를 만들고 실행하는 코드
- ijh 함수는 print('child process', os.getpid()), print('my parent is', os.getppid()) 두 줄의 코드를 실행
같은 작업을 하는 process 생성
code
from multiprocessing import Process
import os
def ijh():
print('같은 작업 중')
print('child process', os.getpid())
if __name__ == '__main__':
print('parent process', os.getpid())
child1 = Process(target=ijh).start()
child2 = Process(target=ijh).start()
child3 = Process(target=ijh).start()
result
parent process 14588
같은 작업 중
child process 15212
같은 작업 중
child process 21876
같은 작업 중
child process 21776
내용이 같은 작업을 하고 있지만 process는 각각 별개로 실행되기 때문에 PID 또한 서로 다르다는 것을 알 수 있습니다.
각기 다른 작업을 하는 porcess 생성
code
from multiprocessing import Process
import os
def I():
print('This is I')
def Joo():
print('This is Joo')
def Han():
print('This is Han')
if __name__ == '__main__':
child1 = Process(target=I).start()
child2 = Process(target=Joo).start()
child3 = Process(target=Han).start()
result
This is I
This is Joo
This is Han
- 위와 같이 process를 사용하여 각기 다른 작업을 동시에 수행하는 code를 작성할 수도 있습니다.
- process들은 각각 독립적으로 실행되므로 서로의 작업에 거의 영향을 미치지 않습니다.
'T.I.L (Today_I_Learned)' 카테고리의 다른 글
T.I.L Apr 24, 2023 poetry 설치 & 가상환경 설정(Dependency Management for Python) (0) | 2023.04.24 |
---|---|
T.I.L Apr 19, 2023 (thread) (0) | 2023.04.19 |
T.I.L - Django 팀 프로젝트 피드백 & 알고리즘 문제 풀이 중 알게 된 (0) | 2023.04.17 |
T.I.L - Django 팀 프로젝트 5 (0) | 2023.04.14 |
T.I.L - Django 팀프로젝트 4 (2) | 2023.04.13 |