파이썬[Python]: threading - is_alive 메서드

파이썬(Phthon): threading 모듈 - is_alive 함수(function)


/// 설명

스레드의 종료 여부를 표현합니다. 스레드가 종료되기 전까지는 True 의 값을 가지고 있습니다. 종료되지 않은 모든 스레드들을 반환하는 enumerate() 함수가 있습니다.

※ 형식
is_alive()

reference
https://docs.python.org/3/library/threading.html#module-threading

/// 예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import threading
import datetime
import time
 
 
def test_thread(i):
    print(f'   ---> test_thread {i} is started!')
    time.sleep(i)
    print(f'   {i}: test_thread - sleep({i}) ---')
    print(f'   ---> Time: {datetime.datetime.now().time()}')
 
 
print(f'---> Time: {datetime.datetime.now().time()}')
= threading.Thread(target=test_thread, args=(1,))
t.start()
print(f'---> Time: {datetime.datetime.now().time()}')
print('-' * 50)
 
time.sleep(1)
 
if t.is_alive() == True:
    print('\nThe thread is still alive')
 
time.sleep(1)
print('...')
time.sleep(1)
print('......')
time.sleep(1)
print('.........')
time.sleep(1)
print('............')
 
t.join()
if t.is_alive() == False:
    print('\nThe thread is terminated')
 
# ---> Time: 21:55:42.912812
#    ---> test_thread 1 is started!
# ---> Time: 21:55:42.913754
# --------------------------------------------------
#    1: test_thread - sleep(1) ---
# The thread is still alive
#    ---> Time: 21:55:43.923536
#
# ...
# ......
# .........
# ............
#
# The thread is terminated
cs

* 실행환경: Microsoft Windows 10 Homes
* 인터프리터: 파이썬(Python 3.9)


– 당신을 응원합니다. –

댓글

이 블로그의 인기 게시물

파이썬[Python]: 내장함수 - from_bytes 메서드

파이썬[Python]: 내장함수 - __len__ 메서드

파이썬[Python]: kivy - 한글 사용

파이썬[Python]: 내장함수 - bit_length 메서드

C 언어: sin 함수, cos 함수, tan 함수