# 我怎樣才能在Python中延遲時間？

## Answers

``````import time
time.sleep(5)   # delays for 5 seconds. You can Also Use Float Value.``````

``````import time
while True:
print("This prints once a minute.")
time.sleep(60)   # Delay for 1 minute (60 seconds).``````

``````from time import sleep
sleep(0.1) # Time in seconds.``````

``````import time
time.sleep(60)``````

``time.sleep(time.localtime(time.time())[5])``

`[5]`只是拉出`time.localtime()`的返回值。

`time.sleep`是它支持浮點數！

``````import time
time.sleep(0.1) ``````

# 自上次測量延遲（定期醒來）

## 蜂鳴器發生器

``````import time
from itertools import count

def buzzergen(period):
nexttime = time.time() + period
for i in count():
now = time.time()
tosleep = nexttime - now
if tosleep > 0:
time.sleep(tosleep)
nexttime += period
else:
nexttime = now + period
yield i, nexttime``````

## 調用常規蜂鳴器

``````from sleepy import buzzergen
import time
buzzer = buzzergen(3) # planning to wake up each 3 seconds
print time.time()
buzzer.next()
print time.time()
time.sleep(2)
buzzer.next()
print time.time()
time.sleep(5) # sleeping a bit longer than usually
buzzer.next()
print time.time()
buzzer.next()
print time.time()``````

``````1400102636.46
1400102639.46
1400102642.46
1400102647.47
1400102650.47``````

``````import random
for ring in buzzergen(3):
print "now", time.time()
print "ring", ring
time.sleep(random.choice([0, 2, 4, 6]))``````

``````now 1400102751.46
ring (0, 1400102754.461676)
now 1400102754.46
ring (1, 1400102757.461676)
now 1400102757.46
ring (2, 1400102760.461676)
now 1400102760.46
ring (3, 1400102763.461676)
now 1400102766.47
ring (4, 1400102769.47115)
now 1400102769.47
ring (5, 1400102772.47115)
now 1400102772.47
ring (6, 1400102775.47115)
now 1400102775.47
ring (7, 1400102778.47115)``````

# 我怎樣才能在Python中延遲時間？

``````>>> from time import sleep

>>> sleep(4)``````

``````>>> def party_time():
...     print('hooray!')
...
>>> sleep(3); party_time()
hooray!``````

“萬歲！” 我按Enter鍵 3秒後打印。

### 使用多個線程和進程的`sleep`示例

``````from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor, as_completed
from time import sleep, time

def party_later(kind='', n=''):
sleep(3)
return kind + n + ' party time!: ' + __name__

def main():
with ProcessPoolExecutor() as proc_executor:
with ThreadPoolExecutor() as thread_executor:
start_time = time()
proc_future1 = proc_executor.submit(party_later, kind='proc', n='1')
proc_future2 = proc_executor.submit(party_later, kind='proc', n='2')
thread_future1 = thread_executor.submit(party_later, kind='thread', n='1')
thread_future2 = thread_executor.submit(party_later, kind='thread', n='2')
for f in as_completed([
proc_future1, proc_future2, thread_future1, thread_future2,]):
print(f.result())
end_time = time()
print('total time to execute four 3-sec functions:', end_time - start_time)

if __name__ == '__main__':
main()``````

``````thread1 party time!: __main__
thread2 party time!: __main__
proc1 party time!: __mp_main__
proc2 party time!: __mp_main__
total time to execute four 3-sec functions: 3.4519670009613037``````

## 多線程

``````>>> from threading import Timer
>>> t = Timer(3, party_time, args=None, kwargs=None)
>>> t.start()
>>>
>>> hooray!

>>> ``````

Python標準庫中的tkinter庫是一個可導入的交互式工具。 基本上，您可以創建按鈕，框和彈出窗口，以及用代碼操作的窗口。

``time.sleep(The amount of time)``

``````time.sleep(1)
print('Hello World!')``````

``````from time import sleep

# doing stuff
sleep(0.5) # sleeping half a second (sleep() uses seconds, but you can also use floats)
# doing stuff..``````

``````from time import sleep
sleep(1)``````

``from time import sleep``

``sleep(seconds)``

``time.sleep()``

``````import time
time.sleep(1)``````

``````import time
time.sleep(5) #delay for 5 seconds.``````

`` driver.implicitly_wait(5)``

``self.wait.until(EC.presence_of_element_located((By.ID, 'UserName'))``

``````import pygame
pygame.init()
while True:
pygame.time.wait(1000)
print("This prints every second.")
print("Please note that this method uses milliseconds.")``````