time.sleep은 스레드 sleep? 프로세스 sleep?

조회수 11152회

리눅스/유닉스에서 파이썬에 time.sleep()은 스레드 하나만 block하나요? 아니면 프로세스 전체를 block하나요?

1 답변

  • 좋아요

    0

    싫어요
    채택 취소하기

    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 하는 걸 확인할 수 있습니다.

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)