do something in time interval

P

Petr Jakes

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.

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

Best regards

Petr Jakes
 
T

Terry Reedy

Petr said:
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.

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

The eventFlag is confusing. Better:

dt = 5 # for instance
event_time = time.time()
while 1:
#do every loop stuff
now = time.time()
if now >= event_time:
event_time = now + dt
event()

If you have multiple delayed events, quite possibly each with a
different or even variable delta, store (future_time,func_to_call) pairs
in a priority queue (heapq module).

Terry Jan Reedy
 
J

Jerry Hill

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.
 
M

Mathieu Prevot

2008/10/6 Petr Jakeš said:
Thanks for your reply.
During the 5s period my script has to do some stuff instead of sleeping.
Thats why it runs in the loop and once in 5s period it has to trigger some
other stuff(function, method, action) to do.
Petr Jakes

You might want to use threads so you have a very simple code like this:

data data0

thread1:
while True:
time.sleep(5)
doSomething(data0)

thread2:
while True:
time.sleep(0.01)
doAnotherthing(data0)

thread1 and thread2 can run in parallel; if thread2 take more time,
thread1 won't be impaired.

Mathieu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top