472,096 Members | 2,215 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

(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 1281
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by S. David Rose | last post: by
1 post views Thread by Andreas Lobinger | last post: by
1 post views Thread by Varun Kacholia | last post: by
7 posts views Thread by Steven D'Aprano | last post: by
2 posts views Thread by Steven D'Aprano | last post: by
13 posts views Thread by LordHog | last post: by
9 posts views Thread by Aaron Watters | last post: by
reply views Thread by Daniel Fetchinson | last post: by

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.