반응형

파이썬에서 실행중인 프로세스 이름과 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

 

 

반응형

문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts