开发者

Executing a function every 1 second [duplicate]

开发者 https://www.devze.com 2023-01-08 07:18 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: Python timer countdown Hi guys,
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Python timer countdown

Hi guys,

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 m开发者_Go百科e how to do this.


You can use a while loop, and the time module, with time.sleep to wait one second, and time.clock to know at what time you print your variables.


You can see, if you can utilize the sched module from the standard library:

The sched module defines a class which implements a general purpose event scheduler.

>>> import sched, time
>>> s = sched.scheduler(time.time, time.sleep)
>>> def print_time(): print "From print_time", time.time()
...
>>> def print_some_times():
...     print time.time()
...     s.enter(5, 1, print_time, ())
...     s.enter(10, 1, print_time, ())
...     s.run()
...     print time.time()
...
>>> print_some_times()
930343690.257
From print_time 930343695.274
From print_time 930343700.273
930343700.276
0

精彩评论

暂无评论...
验证码 换一张
取 消