473,561 Members | 3,336 Online
Bytes | Software Development & Data Engineering Community
+ 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 1364
On Aug 13, 5:16 pm, johnmfis...@com cast.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_FACT OR','5081842980 034330599302211 433031103327124 931395791904635 267920626220458 934262381123664 798988914517309 8650749']]

the_primes = []
the_intractable s = []

phase = 1
the_times = []
while the_composites:
print "="*40
print 'Phase',phase
the_comp = the_composites. pop(0)
print the_comp
print
the_times.appen d(time.time()) # time how long it takes to run
factor!.exe
the_output = os.popen(factor _program+the_co mp[1]).readlines()
the_times.appen d(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.appe nd([new_factors[0][0],long(new_facto rs[0][1])])
else:
the_intractable s.append([new_factors[0][0],long(new_facto rs[0]
[1])])
new_factors.pop ()
while new_factors:
j = new_factors.pop (0)
if j[0] == 'PRIME_FACTOR':
the_primes.appe nd([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_intractable s.sort()
the_primes.exte nd(the_intracta bles)

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

## =============== =============== ==========
## Phase 1
## ['COMPOSITE_FACT OR',
'50818429800343 305993022114330 311033271249313 957919046352679 206262204589342 623811236647989 889145173098650 749']
##
## ['PRIME_FACTOR', '37']
## ['PRIME_FACTOR', '43']
## ['PRIME_FACTOR', '167']
## ['COMPOSITE_FACT OR', '507787751']
## ['PRIME_FACTOR', '69847']
## ['PRIME_FACTOR', '30697']
## ['PRIME_FACTOR', '89017']
## ['PRIME_FACTOR', '3478697']
## ['PRIME_FACTOR', '434593']
## ['PRIME_FACTOR', '49998841']
## ['PRIME_FACTOR', '16161070459714 3']
## ['PRIME_FACTOR', '14064370273']
## ['COMPOSITE_FACT OR', '96303939470359 8565337297']
## ['PRIME_FACTOR', '11927295803']
##
## 0.860000133514 seconds
## =============== =============== ==========
## Phase 2
## ['COMPOSITE_FACT OR', '507787751']
##
## ['PRIME_FACTOR', '29819']
## ['PRIME_FACTOR', '17029']
##
## 0.0780000686646 seconds
## =============== =============== ==========
## Phase 3
## ['COMPOSITE_FACT OR', '96303939470359 8565337297']
##
## ['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.ne twrote:

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*********@com cast.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*********@co mcast.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(nega tive_number) does.

- Hendrik

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

jo*********@com cast.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(nega tive_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
1815
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 http://ip-addr/image.jpg. That's the easy part, but I want to have 2 cameras poll 3-times a second, 4 cameras poll 2 times a second, and the remaining 2...
1
1628
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 that has examples how to use the hotshot for converage analysis and to display timing per line?
9
2993
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. For those that have no idea what I am talking about (I learn a lot reading posts on subjects in which I am clueless), consider the following code...
1
3461
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 "same order" I mean "same order *each time I sort*"). Or is there a random number used in the splitter for qsort? Thanks
7
1156
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. Here are the snippets: def timer1(): timer = time.time func = lambda : None itr = * 1000000
2
3281
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 to use from the command line, but it is less convenient for timing large pieces of code or when working in the interactive interpreter. E.g....
13
11261
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 would implement an interrupt driven model, but the external device (which is connected via USB) does not support interrupts therefore the device...
9
9020
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 and re-sort. The following test case demonstrates this. It can be criticized in many ways: it only tests lists of the same size, it only uses...
0
1919
by: Daniel Fetchinson | last post by:
On 4/15/08, Daniel Fetchinson <fetchinson@googlemail.comwrote: BTW, using the following ###################################################################### # CODE TO TEST BOTH FUNCTIONS back = fill_matrix(generate_zero())
0
7562
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...
0
8074
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...
1
7608
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7926
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...
0
6198
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5465
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...
0
5181
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...
0
3613
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1172
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.