| powernap (version 30) | index /home/james/code/powernap/powernap.so |
Pretty accurate-ish timing in python
Python's time.sleep() is rather bad for accurate timing.
It can sleep for less time than requested, if there's an interrupt.
Or longer, depending on what the scheduler is up to.
The rtc is set to block reads until divisions of 1/2048 seconds (default).
Repeated reading allows accurate "sleeping", although since this is really
repeated locking, it's called "napping", here.
nap(seconds) simply naps for the given period
rnap(seconds) is a padding nap. This for ensuring that the time between
two calls to rnap will be at least "seconds" long. I say at least, because
it could have been longer than the interval since the last call, in which case
there is no nap.
See: http://www.larsen-b.com/Article/221.html
http://www.opengroup.org/pubs/online/7908799/xsh/sched.h.html
http://www.mjmwired.net/kernel/Documentation/rtc.txt
Example 1:
#Simply sleep for a given time
#Low accuracy, low priority setup
powernap.rtc_on(64,5)
#Nap for 1 second
powernap.nap(1)
#the rest of your code
powernap.rtc_off()
Example 2:
#A timing loop that pads to create a fixed length cycle
#Default frequency(2048) and priority (50)
powernap.rtc_on()
while(True):
#Do some stuff
#Then make sure that the cycle takes at least 1.5s
powernap.rnap(1.5)
powernap.rtc_off()
/** powernap - accurate sleeping for python
* Copyright (C) 2006 James Shuttleworth
*
* Contact: james@dis-dot-dat.net
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
| Modules | ||||||
| ||||||
| Functions | ||
| ||
| Data | ||
| __all__ = ['rtc_on', 'rtc_off', 'nap', 'rnap'] __author__ = 'James Shuttleworth' __version__ = '$Revision: 30 $' | ||
| Author | ||
| James Shuttleworth | ||