473,385 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

do something in time interval

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
Oct 6 '08 #1
4 3979
Petr Jakes 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.

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

Oct 6 '08 #2
On Mon, Oct 6, 2008 at 2:07 PM, Petr Jakes <pe************@gmail.comwrote:
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
Oct 6 '08 #3
On Mon, 6 Oct 2008 21:13:21 +0200
"Petr Jake?" <pe********@tpc.czwrote:
I am not an expert, but why not to use time.sleep(5)?
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.
Then check out http://www.python.org/doc/current/library/select.html

--
D'Arcy J.M. Cain <da***@druid.net | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
Oct 6 '08 #4
2008/10/6 Petr Jakeš <pe********@tpc.cz>:
>I am not an expert, but why not to use time.sleep(5)?
If you are using wxPython, you may also try wx.Timer, in which you could
set its interval.

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
Oct 6 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Marcus | last post by:
I am having some problems with trying to perform calculations on time fields. Say I have a start time and an end time, 1:00:00 and 2:30:00 (on a 24 hour scale, not 12). I want to find the...
10
by: Andreas | last post by:
Hi! Is it possible to get a time event at a specific time, for instance eight a'clock? My program is running in the background and is minimized to the tray bar. If not, is there a smooth way...
2
by: androtech | last post by:
Hello, I'm looking for a function that returns a date range for a specified week number of the year. I'm not able to find functions like this anywhere. Any pointers/help would be much...
4
by: Holger Marzen | last post by:
Say, we have uptimes from several servers: Server up_from up_to ------ ------- ------- s1 0:00 8:00 s1 10:00 20:00 s1 22:00 24:00 (would better be...
9
by: HL | last post by:
I am using VS 2005 Beta - C# Problem: The Timer fires a few milliseconds before the actual Due-Time Let's say a timer is created in the following manner: System.Threading.Timer m_timer = null;...
3
by: arief# | last post by:
Dear all, I'm sorry if this sounds stupid or have been talked about before. Suppose I have a field in my table that's called duration with type 'time without timezone'. How do I do sum on...
2
by: dwasbig9 | last post by:
Hi Group (fairly limited knowledge of Access and almost none of Access VBA. Using Access 2003). I need to sum time, I've found through the groups archive an sql extract that led me to this ...
1
by: durai4you | last post by:
Hi IT's, Directly i'm coming to the point. I have Schedule time, Interval and current time. What i want to do is I have to start the process using schedule time and intrevel, For example The...
0
by: Hendrik van Rooyen | last post by:
Lawrence D'Oliveiro wrote: The above is an endless loop, doing something every five seconds, and something else in the in between times. I don't think you are dense - its a good point,...
15
by: student4lifer | last post by:
Hello, I have 2 time fields dynamically generated in format "m/d/y H:m". Could someone show me a good function to calculate the time interval difference in minutes? I played with strtotime() but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.