473,466 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

(sort of) deterministic timing in Python

I am working on a framework for data acquisition in Python 2.5, am
trying to get a structure going more like this:
mark start time
start event
event finishes
count time until next interval
start second event…

rather than this:

start event
event finishes
sleep for interval
start second event

Do you see the difference? I get a true fixed interval from the first,
including the time to accomplish the event task(s). In the second case,
the sleep just gets tacked on at the end of the events, not very
deterministic (timing-wise).

So how do I accomplish this in Python with a minimum of labour?

Thanks for any light you can shed on my darkness...

wave_man
Aug 13 '07 #1
5 1360
On Aug 13, 5:16 pm, johnmfis...@comcast.net (John Fisher) wrote:
I am working on a framework for data acquisition in Python 2.5, am
trying to get a structure going more like this:

mark start time
start event
event finishes
count time until next interval
start second event...

rather than this:

start event
event finishes
sleep for interval
start second event

Do you see the difference? I get a true fixed interval from the first,
including the time to accomplish the event task(s). In the second case,
the sleep just gets tacked on at the end of the events, not very
deterministic (timing-wise).

So how do I accomplish this in Python with a minimum of labour?

Thanks for any light you can shed on my darkness...

wave_man
In the first example, you know when to start the second interval,
right? In the second case, you are assuming the second task is
ready to run after the sleep interval.

Here's an example that times how long an external program takes,
the os.popen() returns to the Python script when the external
C program completes, so no need for any sleep interval.

Because of the poorly written factor program, some composites
are falsely reported as unfactorable. This example keeps
re-calling the factor!.exe program until all composites are
factored or marked intractable, and the time to execute each
iteration of factor!.exe is printed after each call.
import os
import time

factor_program = 'factor! -d200 '

the_composites =
[['COMPOSITE_FACTOR','508184298003433059930221143303 11033271249313957919046352679206262204589342623811 236647989889145173098650749']]

the_primes = []
the_intractables = []

phase = 1
the_times = []
while the_composites:
print "="*40
print 'Phase',phase
the_comp = the_composites.pop(0)
print the_comp
print
the_times.append(time.time()) # time how long it takes to run
factor!.exe
the_output = os.popen(factor_program+the_comp[1]).readlines()
the_times.append(time.time())
new_factors = [i.split() for i in the_output]
for i in new_factors: print i
print
if len(new_factors) == 1:
# it's prime or intractable
if new_factors[0][0] == 'PRIME_FACTOR':
the_primes.append([new_factors[0][0],long(new_factors[0][1])])
else:
the_intractables.append([new_factors[0][0],long(new_factors[0]
[1])])
new_factors.pop()
while new_factors:
j = new_factors.pop(0)
if j[0] == 'PRIME_FACTOR':
the_primes.append([j[0],long(j[1])])
else:
the_composites.append(j)
print the_times[phase] - the_times[phase-1],'seconds'
phase += 1

print "="*40
print
print 'Factoring complete'
print

the_primes.sort()
the_intractables.sort()
the_primes.extend(the_intractables)

for i in the_primes:
print i[0],i[1]
print
print "="*40

## ========================================
## Phase 1
## ['COMPOSITE_FACTOR',
'5081842980034330599302211433031103327124931395791 90463526792062622045893426238112366479898891451730 98650749']
##
## ['PRIME_FACTOR', '37']
## ['PRIME_FACTOR', '43']
## ['PRIME_FACTOR', '167']
## ['COMPOSITE_FACTOR', '507787751']
## ['PRIME_FACTOR', '69847']
## ['PRIME_FACTOR', '30697']
## ['PRIME_FACTOR', '89017']
## ['PRIME_FACTOR', '3478697']
## ['PRIME_FACTOR', '434593']
## ['PRIME_FACTOR', '49998841']
## ['PRIME_FACTOR', '161610704597143']
## ['PRIME_FACTOR', '14064370273']
## ['COMPOSITE_FACTOR', '963039394703598565337297']
## ['PRIME_FACTOR', '11927295803']
##
## 0.860000133514 seconds
## ========================================
## Phase 2
## ['COMPOSITE_FACTOR', '507787751']
##
## ['PRIME_FACTOR', '29819']
## ['PRIME_FACTOR', '17029']
##
## 0.0780000686646 seconds
## ========================================
## Phase 3
## ['COMPOSITE_FACTOR', '963039394703598565337297']
##
## ['PRIME_FACTOR', '518069464441']
## ['PRIME_FACTOR', '1858900129817']
##
## 0.0469999313354 seconds
## ========================================
##
## Factoring complete
##
## PRIME_FACTOR 37
## PRIME_FACTOR 43
## PRIME_FACTOR 167
## PRIME_FACTOR 17029
## PRIME_FACTOR 29819
## PRIME_FACTOR 30697
## PRIME_FACTOR 69847
## PRIME_FACTOR 89017
## PRIME_FACTOR 434593
## PRIME_FACTOR 3478697
## PRIME_FACTOR 49998841
## PRIME_FACTOR 11927295803
## PRIME_FACTOR 14064370273
## PRIME_FACTOR 518069464441
## PRIME_FACTOR 1858900129817
## PRIME_FACTOR 161610704597143
##
## ========================================

Aug 13 '07 #2
"John Fisher" <jo.....cast.netwrote:

import time
period_time = TIME_CONSTANT # The time of a period in seconds - 0.001 is a
millisec
>mark start time
start_time = time.time()
>start event
event finishes
event_time = time.time() - start_time
wait_time = period_time-event_time
>count time until next interval
if wait_time 0:
time.sleep(wait_time)
>start second event…
that should (sort of) do it.

HTH - Hendrik
Aug 14 '07 #3
jo*********@comcast.net (John Fisher) writes:
mark start time
start event
event finishes
count time until next interval
start second event…

rather than this:

start event
event finishes
sleep for interval
start second event
...
So how do I accomplish this in Python with a minimum of labour?
Normally I'd use something like:

from time import time

t0 = time()
start event ... event finishes
t1 = time()
elapsed = t1 - t0
sleep(interval - elapsed)
start second event ...

Am I missing something?
Aug 16 '07 #4

"Paul Rubin" <http://p..idwrote:

>jo*********@comcast.net (John Fisher) writes:
>mark start time
start event
event finishes
count time until next interval
start second event…

rather than this:

start event
event finishes
sleep for interval
start second event
...
So how do I accomplish this in Python with a minimum of labour?

Normally I'd use something like:

from time import time

t0 = time()
start event ... event finishes
t1 = time()
elapsed = t1 - t0
sleep(interval - elapsed)
start second event ...

Am I missing something?
Not much - only beware of cases when elapsed is greater than
interval - not sure what time.sleep(negative_number) does.

- Hendrik

Aug 17 '07 #5
On 8/17/07, Hendrik van Rooyen <ma**@microcorp.co.zawrote:
>
"Paul Rubin" <http://p..idwrote:

jo*********@comcast.net (John Fisher) writes:
mark start time
start event
event finishes
count time until next interval
start second event…

rather than this:

start event
event finishes
sleep for interval
start second event
...
So how do I accomplish this in Python with a minimum of labour?
Normally I'd use something like:

from time import time

t0 = time()
start event ... event finishes
t1 = time()
elapsed = t1 - t0
sleep(interval - elapsed)
start second event ...

Am I missing something?

Not much - only beware of cases when elapsed is greater than
interval - not sure what time.sleep(negative_number) does.
On Windows 2k3, Python 2.5 it sleeps forever (or almost forever? Maybe
a signed/unsigned thing) so yeah, be careful of it.
Aug 17 '07 #6

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

Similar topics

6
by: S. David Rose | last post by:
Hello All! I am new to Python, and wanted to know if I might ask you a question regarding timing. I want a main loop which takes photos from 8 different web-cams, each can be addressed by...
1
by: Andreas Lobinger | last post by:
Aloha, hotshot.Profile has flags for recording timing per line and line events. Even if i had both set to 1 i still get only the standard data (time per call). Is there any document available...
9
by: plahey | last post by:
I have been dabbling in Python for a while now. One of the things that really appeals to me is that I can seem to be able to use C++-style RAII idioms to deal with resource management issues. ...
1
by: Varun Kacholia | last post by:
Hi, I have a question regarding SGI STL sort implementation: In case of equal elements, will they be output in the same order each time I sort? (I understand that it is not a stable sort, and by...
7
by: Steven D'Aprano | last post by:
I have two code snippets to time a function object being executed. I expected that they should give roughly the same result, but one is more than an order of magnitude slower than the other. ...
2
by: Steven D'Aprano | last post by:
The timeit module is ideal for measuring small code snippets; I want to measure large function objects. Because the timeit module takes the code snippet argument as a string, it is quite handy...
13
by: LordHog | last post by:
Hello all, I have a little application that needs to poll a device (CAN communications) every 10 to 15 ms otherwise the hardware buffer might overflow when there are message burst on the bus. I...
9
by: Aaron Watters | last post by:
....is to forget they are sorted??? While trying to optimize some NUCULAR libraries I discovered that the best way to merge 2 sorted lists together into a new sorted list is to just append them...
0
by: Daniel Fetchinson | last post by:
On 4/15/08, Daniel Fetchinson <fetchinson@googlemail.comwrote: BTW, using the following ###################################################################### # CODE TO TEST BOTH...
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
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...
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
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,...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.