time.sleep은 스레드 sleep? 프로세스 sleep?
조회수 10909회
1 답변
-
time.sleep에서 적혀있길
Suspend execution of the calling thread for the given number of seconds.
라고 합니다. 스레드만 블록하네요 실제로 그런지 실험해보면
# coding=utf-8 import thread, time, datetime def counter(id, timeToSleep): print "스레드 %d진입. %s만큼 sleep" %(id, timeToSleep) time.sleep(timeToSleep) print "스레드 %d, 현재시간:%s" %(id, datetime.datetime.now().time()) print "시작 시간은 :", datetime.datetime.now().time() thread.start_new_thread(counter, (0, 10,)) thread.start_new_thread(counter, (1, 10,)) time.sleep(20) #스레드가 다 끝나기 전까지 기다림
결과 :
시작 시간은 : 16:19:51.790819 스레드 0진입. 10만큼 sleep 스레드 1진입. 10만큼 sleep 스레드 0, 현재시간:16:20:01.793599 스레드 1, 현재시간:16:20:01.793714
각 스레드가 자기가 sleep 할 시간만큼만 sleep 하는 걸 확인할 수 있습니다.
댓글 입력