반응형

스레드가 종료되거나 죽었다면 다시 스레드를 실행시켜주는 파이썬 예제 코드입니다. 

 

2023. 1. 28  최초작성

 

참고

https://blog.naver.com/ytlee64/222832101970 




import threading


def func():
    for i in range(5):
        print(f'##### thread print {i}')

    print('##### thread exit #####')
       

t = threading.Thread(target=func)
t.start()


# 스레드 재시작을 2번 반복합니다.
try_num = 2

while True:
    if try_num == 0:
        break
   
    # 스레드가 살아있는지 체크   
    if t!=None and t.is_alive():
        #print('thread alive')
        continue

    # 스레드가 종료되었다면 다시 실행
    else:
        print('thread dead, restart thread')
        t = threading.Thread(target=func)
        t.start()

        try_num = try_num -1



실행 결과입니다. 스레드가 2번 재시작되었습니다.

 

##### thread print 0

##### thread print 1

##### thread print 2

##### thread print 3

##### thread print 4

##### thread exit #####

thread dead, restart thread

##### thread print 0

##### thread print 1

##### thread print 2

##### thread print 3

##### thread print 4

##### thread exit #####

thread dead, restart thread

##### thread print 0

##### thread print 1

##### thread print 2

##### thread print 3

##### thread print 4

##### thread exit #####


반응형

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

도움이 되셨다면 토스아이디로 후원해주세요.
https://toss.me/momo2024


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

+ Recent posts