파이썬[Python]: threading - Thread 클래스

파이썬(Phthon): threading 모듈 - Thread 클래스(class)


/// 설명

구분되어진 스레드를 제어하는 클래스입니다.(2가지 방식: 콜러블 객체를 전달, run() 메서드의 오버라이딩)

참고: 스레드
group: 사용하지 않습니다.
target: run() 메서드에 의해 시행되는 콜러블 객체입니다.
name: 스레드에 이름을 부여합니다. 기본값은 Thread-N, N은 정수
args: target 에 사용되어지는 튜플입니다.
kwargs: target 에 사용되어지는 딕셔너리입니다.
deamon: the thread is daemonic. (백그라운드에서 시행되는 듯...)

※ 형식
class threading.Thread(group=None, target=None, name=None, args=(), kwargs={}, *, daemon=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('   ---> Time:', datetime.datetime.now().time())
 
 
print('---> Time:', datetime.datetime.now().time())
for i in range(14):
    t = threading.Thread(target=test_thread, args=(i,))
    t.start()
print('---> Time:', datetime.datetime.now().time())
print('-' * 50)
 
# ---> Time: 18:13:00.224742
#    ---> test_thread 1 is started!
#    ---> test_thread 2 is started!
#    ---> test_thread 3 is started!
# ---> Time: 18:13:00.226736
# --------------------------------------------------
#    1: test_thread - sleep(1) ---
#    ---> Time: 18:13:01.239569
#    2: test_thread - sleep(2) ---
#    ---> Time: 18:13:02.237071
#    3: test_thread - sleep(3) ---
#    ---> Time: 18:13:03.240688
cs

/// 예제 name

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
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('   ---> Time:', datetime.datetime.now().time())
 
# a name of the thread
print('---> Time:', datetime.datetime.now().time())
= threading.Thread(name='Thread_example', target=test_thread, args=(0,))
print('Name:', t.name)
t.start()
print('---> Time:', datetime.datetime.now().time())
print('-' * 50)
 
# ---> Time: 18:14:05.829778
# Name: Thread_example
#    ---> test_thread 0 is started!
# ---> Time: 18:14:05.830778
# --------------------------------------------------
#    0: test_thread - sleep(0) ---
#    ---> Time: 18:14:05.830778
 
cs

/// 예제 daemon

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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('   ---> Time:', datetime.datetime.now().time())
 
# non daemon thread exits when main thread exit, daemon -> possible to alive(?)
print('---> Time:', datetime.datetime.now().time())
= threading.Thread(target=test_thread, args=(3,), daemon=True)
t.start()
print('---> Time:', datetime.datetime.now().time())
print('-' * 50)
 
# ---> Time: 18:15:23.656830
#    ---> test_thread 3 is started!
# ---> Time: 18:15:23.657827
# --------------------------------------------------
cs

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


– 당신을 응원합니다. –

댓글

이 블로그의 인기 게시물

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

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

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

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

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