By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,768 Members | 2,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,768 IT Pros & Developers. It's quick & easy.

(sort of) deterministic timing in Python

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a

"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

P: n/a
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.