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