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

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


/// 설명

스레드가 종료되기까지 기다립니다. 이 메서드는 timeout 이 발생되거나 join() 메서드가 호출되는 스레드가 종료하기 전까지 스레드 호출을 차단합니다. 여러번 사용될 수 있습니다.

timeout: 실수(float)로 스레드가 종료되는 시간을 명시합니다. 종료 여부는 is_alive()로 확인할 수 있습니다.

※ 형식
join(timeout=None)

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
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)
 
t.join()
if t.is_alive() == False:
    print('\nThe thread is terminated')
 
# ---> Time: 21:28:45.390469
#    ---> test_thread 1 is started!
# ---> Time: 21:28:45.390469
# --------------------------------------------------
#    1: test_thread - sleep(1) ---
#    ---> Time: 21:28:46.404323
# 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 함수