Python 시간 sleep() 함수는 지정된 시간(초) 동안 실행을 일시 중지합니다.
시간 수면 구문()
구문: 수면(초)
매개변수:
sec : 코드를 중지해야 하는 시간(초)입니다.
반품 : 무효의.
때로는 다른 여러 실행이 수행되거나 단순히 필요한 유틸리티로 인해 프로그램 흐름을 중단해야 하는 경우도 있습니다. sleep()은 일정 기간 동안 코드 흐름을 중지하는 정확하고 유연한 방법을 제공하는 상황에서 유용할 수 있습니다. 이 기능은 이 기능에 대한 통찰력을 논의합니다.
예시 1: 시간 지연 생성 초
시작 시간과 종료 시간은 6초 지연되어 인쇄됩니다.
json 파일을 여는 방법
파이썬3
import> time> # printing the start time> print>(>'The time of code execution begin is : '>, time.ctime())> # using sleep() to hault the code execution> time.sleep(>6>)> # printing the end time> print>(>'The time of code execution end is : '>, time.ctime())> |
>
>
산출:
The time of code execution begin is : Mon Apr 9 20:57:10 2018 The time of code execution end is : Mon Apr 9 20:57:16 2018>
예시 2: 시간 지연 생성 분
3분 후에 목록이 표시됩니다.
파이썬3
import> time> # creating and Initializing a list> Languages>=> [>'Java'>,>'C++'>,>'Python'>,>'Javascript'>,> >'C#'>,>'C'>,>'Kotlin'>]> # creating a time delay of 3 minutes> time.sleep(>3> *> 60>)> print>(Languages)> |
>
>
산출:
3분 후에 목록이 다음과 같이 표시됩니다.
['Java', 'C++', 'Python', 'Javascript', 'C#', 'C', 'Kotlin']>
time.sleep() 적용
sleep()이 사용되는 응용프로그램은 많습니다. 일정한 간격으로 반복되는 백그라운드 스레드 실행은 sleep()의 도움으로 구현될 수 있습니다. 또 다른 인기 있는 응용 프로그램은 좋은 사용자 인터페이스를 위해 sleep()을 사용하여 단어를 한 글자씩 인쇄하는 것입니다. 후자는 아래 코드에 표현되어 있습니다.
예시 1: 시간 지연 생성 파이썬 루프
파이썬3
import> time> # initializing string> strn>=> 'techcodeview.com'> # printing geeksforgeeks after delay> # of each character> for> i>in> range>(>0>,>len>(strn)):> >print>(strn[i], end>=>'')> >time.sleep(>2>)> |
>
>
산출:
GeeksForGeeks>
메모: sleep()의 가시적인 효과는 로컬 편집기에서 볼 수 있습니다.
예시 2: 시간 지연 생성 파이썬 목록
파이썬3
인접각
# importing time package> import> time> # creating a time delay of 5 seconds> time.sleep(>5>)> # creating and Initializing a list> myList>=> [>'Jai'>,>'Shree'>,>'RAM'>,>5>,>'August'>,>2020>]> # the list will be displayed after the> # delay of 5 seconds> print>(myList)> |
>
>
산출:
5초의 지연 후 다음과 같은 출력을 얻게 됩니다.
['Jai', 'Shree', 'RAM', 5, 'August', 2020]>
예시 3: 시간 지연 생성 파이썬 튜플
파이썬3
# importing time package> import> time> # creating a time delay of 4 seconds> time.sleep(>4>)> # creating and Initializing a tuple> mytuple>=> (>'Anil Kumbl'>,>'Sachin Tendulkar'>,>'Sunil Gavaskar'>,> >'Rahul Dravid'>,>'Mahendra Singh Dhoni'>,> >'Dennis Lillee'>,>'Muttiah Muralitharan'>,>'Shane Warne'>)> # the tuple will be displayed after the delay of 4 seconds> print>(mytuple)> |
>
>
산출:
4초의 지연 후 다음과 같은 출력을 얻게 됩니다.
('Anil Kumbl', 'Sachin Tendulkar', 'Sunil Gavaskar', 'Rahul Dravid', 'Mahendra Singh Dhoni', 'Dennis Lillee', 'Muttiah Muralitharan', 'Shane Warne')> 예시 4: 시간 지연 목록 이해
파이썬3
# importing time package> import> time> # creating and Initializing a list> cricketers>=> [>'Anil Kumble'>,>'Sachin Tendulkar'>,>'Sunil Gavaskar'>,> >'Rahul Dravid'>,>'Mahendra Singh Dhoni'>,> >'Dennis Lillee'>,>'Muttiah Muralitharan'>,>'Shane Warne'>]> # time delay of 7 seconds is created> # after every 7 seconds item of list gets displayed> cricketers>=> [(time.sleep(>7>),>print>(cric))>for> cric>in> cricketers]> |
>
>
산출:
7초마다 목록의 항목이 다음과 같이 표시됩니다.
Anil Kumble Sachin Tendulkar Sunil Gavaskar Rahul Dravid Mahendra Singh Dhoni Dennis Lillee Muttiah Muralitharan Shane Warne>
예시 5: 만들기 다수의 시간 지연
파이썬3
# importing time package> import> time> # creating and Initializing a list> Languages>=> [>'Java'>,>'C++'>,>'Python'>,>'Javascript'>,>'C#'>,>'C'>,>'Kotlin'>]> # creating a time delay of 5 seconds> time.sleep(>5>)> # the list will be displayed after the delay of 5 seconds> print>(Languages)> for> lan>in> Languages:> ># creating a time delay of 13 seconds> >time.sleep(>13>)> ># After every 13 seconds an item of list will be displayed> >print>(lan)> |
>
>
자바 xor
산출:
5초 후에 목록이 다음과 같이 표시됩니다.
['Java', 'C++', 'Python', 'Javascript', 'C#', 'C', 'Kotlin']>
그런 다음 13초마다 목록의 항목이 다음과 같이 표시됩니다.
Java C++ Python Javascript C# C Kotlin>