파이썬에서 실행중인 프로세스 pid와 이름 출력하기Python/Python 예제 코드2023. 10. 10. 22:27
Table of Contents
반응형
파이썬에서 실행중인 프로세스 이름과 pid를 출력하는 예제 코드입니다.
2022. 8. 6. 최초작성
from multiprocessing import Process def count(num): while num > 0: num = num - 1 print(num) print('process exit') if __name__ == '__main__': # Process를 사용하려면 꼭 적어줘야 합니다. t = Process(target=count, args=(10,)) t.start() # 실행중인 프로세스 리스트를 출력합니다. import psutil current_process = psutil.Process() children = current_process.children(recursive=True) for child in children: print('Child pid is {}, name {}'.format(child.pid, child.name)) t.join() print('main exit') |
실행 결과입니다.
Child pid is 80968, name <bound method Process.name of psutil.Process(pid=80968, name=’Python’, status=’running’, started=’11:15:58′)>
Child pid is 80969, name <bound method Process.name of psutil.Process(pid=80969, name=’Python’, status=’running’, started=’11:15:58′)>
9
8
7
6
5
4
3
2
1
0
process exit
main exit
참고
https://stackoverflow.com/a/38509696
반응형
'Python > Python 예제 코드' 카테고리의 다른 글
dask 사용해보기 (0) | 2023.10.11 |
---|---|
Process, Thread, Main 함수 로그를 파일에 저장하는 logging Python 예제 (0) | 2023.10.11 |
Python에서 Ctrl+C 감지하기 (0) | 2023.10.08 |
csv 파일을 여러 개 파일로 분리하는 python 예제 (0) | 2023.10.07 |
리스트를 정해진 개수로 분할하여 딕셔너리에 저장하는 Python 예제 (0) | 2023.01.21 |