473,400 Members | 2,145 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,400 software developers and data experts.

Re: re : do something in time interval

Lawrence D'Oliveiro wrote:
>Hendrik van Rooyen wrote:
> import time
while True:
end_time = time.time() + 5
while time.time() < end_time:
do_the_in_between_stuff()
do_the_every_five_second_stuff()

Maybe I'm dense, but ... where do you stop the busy-waiting?
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,
leading to the following analysis:

The do_the_in_between_stuff call either takes
less than, or more than, or exactly five
seconds to do.

In the unlikely exact case, there is no problem, we
have a synchronous machine. (assuming the every five
seconds code takes zero time - else five has to
be diminished to 5 less the time of the periodic
routine, but the argument stays the same)

If it takes more than five seconds, we fail in our
endeavour to do something every five seconds,
and we need threads or interrupts or something else
more advanced to get some apparent simultaneity.

If it takes less than five seconds, we will enter it
more than once. I wrote the above under the twin assumptions
that multiple entry does not matter, and that the routine
is short enough in time that the overrun error at the end
does not significantly change the timing of the five second
interval call. If it perversely takes say 4 seconds to do
the in between stuff, the above code will do the five second
stuff at intervals of eight seconds or so. - Conversely, if the
in between code takes a millisec, then we will be almost
spot-on.

Now if multiple entry does matter, and it has to be done
only once in between calls to the five second stuff,
then it properly belongs with the five second stuff,
and we are back to sleeping for
time.sleep(5 - time_to_do_the_work) to get a call every
five seconds. Obviously, if time_to_do_the_work is
bigger than 5, then you can't get there from here.

And these, as far as I can see, are the only alternatives.

- Hendrik


Oct 9 '08 #1
0 1176

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...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.