파이썬 멀티쓰레딩 하나의 쓰레드 종료 감지
조회수 4291회
def th1():
a = 0
for i in range(10):
a = a + 1
def th2():
while True:
print("Loading")
이렇게 두 가지 함수가 있을 때 th1 함수와 th2함수를 멀티쓰레딩으로 같이 돌린다고 가정합니다. 이때 th1함수가 실행되고있는동안만 th2함수를 실행하고있다가 th1함수의 연산이 모두 종료되면 th2함수를 자동 종료시킬 방법이 있을까요?
-
(•́ ✖ •̀)
알 수 없는 사용자 - 〉
1 답변
-
is_alive
로 쓰레드가 아직 실행중인지 확인할 수 있네요.import threading import time def th1(): print('th1 started.') time.sleep(3) print('\nth1 exiting.') def th2(t): print('th2 started.') while True: if not t.is_alive(): break print('.', end='', flush=True) time.sleep(.3) print('\nth2 exiting.') t1 = threading.Thread(target=th1) t2 = threading.Thread(target=th2, args=(t1,)) t1.start() t2.start()
댓글 입력