473,657 Members | 2,507 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_betwe en_stuff()
do_the_every_fi ve_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_betwe en_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 1193

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

Similar topics

2
6089
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 difference in minutes, divide this result by a predefined size of interval, and make a loop that runs this many times. For example, with an interval size of 15 minutes, it will return 6 blocks... I have this all working fine, but am getting stuck on...
10
24470
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 to accomplish this in a different way? Best regards, Andreas Lundgren
2
5210
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 appreciated. TIA
4
1820
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 a timestamp with 0:00 and next day) s2 0:00 4:00 s2 6:00 23:00
9
7266
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; Let's declare a constant Int32 m_TimePeriod = 10000;
3
8908
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 this field based on another field let say called dateofevent? SQL: SELECT SUM(duration) FROM durtable GROUP BY dateofevent;
2
7593
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 SELECT Format(Sum(Table2.time),"Short Time") AS SumOftime FROM Table2; Which works fine but the article also said I would need a Function to
1
1344
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 schedule time is 5 PM and Interval is 2 Hrs and Current time is 7:30 PM then what i have to do is i have to calculate the wait time to initiate the proces. In this case i have to start the process by 5 PM and next is 7 PM ann then 9 PM and so on......
15
6420
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 but that only gave me difference in hours and only if the times were on the same day (after wrapping with date() function). TIA
0
8425
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8326
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8845
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8743
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8622
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5647
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2745
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1736
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.