Python定时器倒计时

发布于 2024-09-11 03:27:14 字数 252 浏览 5 评论 0原文

我想了解Python中的定时器。

假设我有一个类似以下的代码片段:

def abc()
   print 'Hi'  
   print 'Hello'
   print 'Hai'

我想每 1 秒打印一次。最多三次;即;第一秒我需要检查 printf,第二秒我也需要在第三秒检查。

在我的实际代码中,变量值将被更新。 我需要捕获所有变量在哪一秒更新。

谁能告诉我该怎么做。

I want to know about timer in Python.

Suppose i have a code snippet something like:

def abc()
   print 'Hi'  
   print 'Hello'
   print 'Hai'

And i want to print it every 1 second. Max three times;ie; 1st second i need to check the printf, 2nd second I need to check as well in 3rd second.

In my actual code variables value will be updated.
I need to capture at what second all the variables are getting updated.

Can anybody tell me how to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

紫轩蝶泪 2024-09-18 03:27:14

time.sleep 在这种情况下没问题,但是如果 abc() 函数需要半秒才能执行怎么办?还是5分钟?
在这种情况下,您应该使用 Timer 对象。

from threading import Timer

def abc():
    print 'Hi'  
    print 'Hello'
    print 'Hai'

for i in xrange(3):
    Timer(i, abc).start()

time.sleep is fine in this case but what if the abc() function takes half a second to execute? Or 5 minutes?
In this case you should use a Timer object.

from threading import Timer

def abc():
    print 'Hi'  
    print 'Hello'
    print 'Hai'

for i in xrange(3):
    Timer(i, abc).start()
归途 2024-09-18 03:27:14

使用time.sleep

import time

def abc():
    print 'Hi'
    print 'Hello'
    print 'Hai'

for i in xrange(3):
    time.sleep(1)
    abc()   

Use time.sleep.

import time

def abc():
    print 'Hi'
    print 'Hello'
    print 'Hai'

for i in xrange(3):
    time.sleep(1)
    abc()   
梦回梦里 2024-09-18 03:27:14

您应该查看time.sleep()。例如:

for i in xrange(5):
  abc()
  time.sleep(3)

这将打印您的行 5 次,中间有 3 秒的延迟。

You should look into time.sleep(). For example:

for i in xrange(5):
  abc()
  time.sleep(3)

That will print your lines 5 times with a 3 second delay between.

千纸鹤带着心事 2024-09-18 03:27:14
import time
def abc()
    for i in range(3):
        print 'Hi'  
        print 'Hello'
        print 'Hai'
        time.sleep(1)
import time
def abc()
    for i in range(3):
        print 'Hi'  
        print 'Hello'
        print 'Hai'
        time.sleep(1)
清旖 2024-09-18 03:27:14
import time
def abc():
 print 'Hi'
 print 'Hello'
 print 'Hai'

for i in range(3):
 time.sleep(3-i)
 abc()
import time
def abc():
 print 'Hi'
 print 'Hello'
 print 'Hai'

for i in range(3):
 time.sleep(3-i)
 abc()
天邊彩虹 2024-09-18 03:27:14

通常对我来说这有效......

import time

def abc():
    print 'Hi'
    time.sleep(1)
    print 'Hello'
    time.sleep(1)
    print 'Hai'
    time.sleep(1)

我想你可以猜到......

usually for me this works...

import time

def abc():
    print 'Hi'
    time.sleep(1)
    print 'Hello'
    time.sleep(1)
    print 'Hai'
    time.sleep(1)

I think you can guess after that...

醉城メ夜风 2024-09-18 03:27:14
import sys
import time

c=':'
sec = 0
min = 0
hour = 0

#count up clock

while True:
for y in range(59):                                                 #hours
    for x in range (59):                                            #min
        sec = sec+1
        sec1 = ('%02.f' % sec)                                      #format
        min1 = ('%02.f' % min)
        hour1= ('%02.f' % hour)
        sys.stdout.write('\r'+str(hour1)+c+str(min1)+c+str(sec1))   #clear and write
        time.sleep(1)
    sec = 0
    sys.stdout.write('\r' + str(hour1) + c + str(min1) + c + '00')  #ensure proper timing and display
    time.sleep(1)
    min=min+1
min = 0
sys.stdout.write('\r' + str(hour1) + c + str(min1) + c + '00')      #ensure proper timing and display
time.sleep(1)
hour=hour+1
import sys
import time

c=':'
sec = 0
min = 0
hour = 0

#count up clock

while True:
for y in range(59):                                                 #hours
    for x in range (59):                                            #min
        sec = sec+1
        sec1 = ('%02.f' % sec)                                      #format
        min1 = ('%02.f' % min)
        hour1= ('%02.f' % hour)
        sys.stdout.write('\r'+str(hour1)+c+str(min1)+c+str(sec1))   #clear and write
        time.sleep(1)
    sec = 0
    sys.stdout.write('\r' + str(hour1) + c + str(min1) + c + '00')  #ensure proper timing and display
    time.sleep(1)
    min=min+1
min = 0
sys.stdout.write('\r' + str(hour1) + c + str(min1) + c + '00')      #ensure proper timing and display
time.sleep(1)
hour=hour+1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文