728x90
반응형
728x170
import threading
import time
class TestThread(threading.Thread):
def __init__(self, message):
threading.Thread.__init__(self)
self.message = message
self.daemon = True
self.event = threading.Event()
def run(self):
while True:
if self.event.is_set():
break;
time.sleep(1)
print(self.message)
threadList = []
for message in ["you", "need", "python"]:
thread = TestThread(message)
threadList.append(thread)
thread.start()
for i in range(10):
time.sleep(1)
print(i)
for thread in threadList:
thread.event.set()
time.sleep(1)
"""
python
need
you
0
1
python
you
need
2
need
python
you
you
3
python
need
python
you
need
4
need
you
5
python
you
need
6
python
need
7
you
python
8
need
you
python
9
need
you
python
"""
728x90
반응형
그리드형(광고전용)
'Python > threading' 카테고리의 다른 글
[PYTHON/THREADING] Lock 클래스 : acquire/release 메소드 사용하기 (0) | 2022.09.20 |
---|---|
[PYTHON/THREADING] Thread 클래스 : 스레드 사용하기 (0) | 2017.12.16 |
댓글을 달아 주세요