파이썬[Python]: threading - run 함수
파이썬(Phthon): threading 모듈 - run 함수(function)
/// 설명
스레드를 동작시킵니다. Thread 클래스의 하위 클래스를 생성하고 run() 메서드를 작성하셔야 합니다. ---> start() 메서드를 사용하여 스레드를 제어합니다.
※ 형식
run()
reference
https://docs.python.org/3/library/threading.html#module-threading
※ 형식
run()
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 | import threading import datetime import time class TestThread(threading.Thread): def __init__(self, *args, **kwargs): super(TestThread, self).__init__(*args, **kwargs) self.i = kwargs['args'][0] def run(self): print(f' ---> test_thread {self.i} is started!') time.sleep(self.i) print(f' {self.i}: test_thread - sleep({self.i}) ---') print(' ---> Time:', datetime.datetime.now().time()) print('---> Time:', datetime.datetime.now().time()) for i in range(1, 4): t = TestThread(args=(i, )) t.start() print('---> Time:', datetime.datetime.now().time()) print('-' * 50) # ---> Time: 19:37:58.046082 # ---> test_thread 1 is started! # ---> test_thread 2 is started! # ---> test_thread 3 is started! # ---> Time: 19:37:58.047080 # -------------------------------------------------- # 1: test_thread - sleep(1) --- # ---> Time: 19:37:59.056648 # 2: test_thread - sleep(2) --- # ---> Time: 19:38:00.051945 # 3: test_thread - sleep(3) --- # ---> Time: 19:38:01.059141 # # Process finished with exit code 0 | cs |
* 실행환경: Microsoft Windows 10 Homes
* 인터프리터: 파이썬(Python 3.9)
– 당신을 응원합니다. –
댓글
댓글 쓰기