파이썬에서 실행중인 프로세스 pid와 이름 출력하기Python/Python 예제 코드2022. 8. 6. 11:18
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
How do you list all child processes in python?
I'm using a third party library that starts various sub processes. When there's an exception I'd like to kill all the child processes. How can I get a list of child pids?
stackoverflow.com
반응형
'Python > Python 예제 코드' 카테고리의 다른 글
lambda와 map을 사용하여 list 원소에 똑같은 문자열을 추가하는 Python 예제 (0) | 2022.10.23 |
---|---|
Python 예제 - ConfigParser를 사용하여 INI 파일을 읽고 쓰기 (0) | 2022.10.22 |
파이썬 예제 - 리스트에 있는 문자열을 조합하기 (0) | 2022.07.12 |
파이썬 예제 - 지정한 경로에서 파일 내용 검색하기 (0) | 2022.07.09 |
파이썬 딕셔너리에 함수 추가해놓고 호출하기 (0) | 2022.07.09 |