do something in time interval
Jerry Hill
malaclypse2 at gmail.com
Mon Oct 6 14:55:55 EDT 2008
On Mon, Oct 6, 2008 at 2:07 PM, Petr Jakes <petr.jakes.tpc at gmail.com> wrote:
> I have infinitive loop running script and I would like to check
> something periodically after 5 seconds (minutes, hours...) time period
> (I do not mean time.sleep(5) ). Till now, I have following script, but
> I think there must be something more elegant.
Take a look at the sched module.
http://www.python.org/doc/2.5.2/lib/module-sched.html
> eventFlag = False
> while 1:
> time.sleep(0.01)
> seconds = time.time()
> if not int(seconds % (5)):
> if eventFlag:
> print "5 seconds, hurray"
> eventFlag = False
> else:
> eventFlag = True
Using sched, I think you would re-write this as:
import sched, time
s=sched.scheduler(time.time, time.sleep)
def do_event():
print "5 seconds, hurray!"
s.enter(5, 1, do_event, ())
s.enter(5, 1, do_event, ())
s.run()
That will run do_event() after five seconds, and then do_event() puts
itself back in the queue to be executed in another 5 seconds.
--
Jerry
More information about the Python-list
mailing list