Pythonのtime.sleep()
Python time sleep() 関数は、指定された秒数の間実行を一時停止します。
time sleep() の構文
構文: スリープ(秒)
パラメーター :
sec : コードを停止する必要がある秒数。
戻り値 : 空所。
場合によっては、他のいくつかの実行を行うため、または単に必要なユーティリティのために、プログラムのフローを停止する必要があることがあります。このような状況では、任意の期間コード フローを停止するための正確かつ柔軟な方法を提供する sleep() が役立ちます。この関数では、この関数の洞察について説明します。
例 1: 時間遅延の作成 秒
開始時刻と終了時刻は 6 秒遅れて印刷されます。
Python3
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分遅れてリストが表示されます
Python3
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() の助けを借りて実装できます。もう 1 つの人気のあるアプリケーションは、優れたユーザー インターフェイスを実現するために sleep() を使用して単語を 1 文字ずつ出力することです。後者は以下のコードで表されます。
例 1: 時間遅延の発生 Python ループ
Python3
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: 時間遅延の発生 パイソン リスト
Python3
# 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: 時間遅延の発生 パイソン タプル
Python3
# 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: 時間の遅れ リストの内包表記
Python3
# 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: 作成 複数 時間の遅れ
Python3
# 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)> |
出力:
5 秒の遅延の後、リストは次のように表示されます。
['Java', 'C++', 'Python', 'Javascript', 'C#', 'C', 'Kotlin']
その後、13 秒ごとに、リスト上の項目が次のように表示されます。
Java C++ Python Javascript C# C Kotlin