반응형

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

 

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://freewriting2024.tistory.com


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

+ Recent posts