[Tutor] better resolution on time.sleep()?

Roger Merchberger zmerch at 30below.com
Tue May 24 00:46:28 CEST 2005


I'm running an application that has a polling loop to check a serial port 
for certain signals, and on my laptop I can get about 6700 samples per 
second, which (of course) consumes 100% CPU; which may impact battery life. 
BTW, I'm running Python 2.2.2 on my laptop... not had a need to upgrade yet.

I really only need between 500 and 1000 samples per second, but as the 
smallest sleep available normally is time.sleep(.01) -- which brings my 
samples/sec to a nice 100; but I need it a little faster.

I found this:

http://freshmeat.net/projects/rtsched/

which is supposed to bring real-time scheduling to Python (which the docs 
say is dangerous if used incorrectly) and also implementations of 
msleep/usleep/nanosleep.

I downloaded it, compiled it, installed it, and popped in an import rtsched 
at the top and rtsched.msleep(1) where the time.sleep(.01) used to go... 
and now I get around 50 samples per second. I tried the usleep(1) and 
nanosleep(1) and the same thing happens. The rtsched implementation *is* 
working, as I can put in rtsched.msleep(100) and my samples/sec drops to 10.

I'm guessing there's so much overhead to calling rtsched that it's b0rking 
up the works. :-/

Anyone know of:

1) a working millisleep/microsleep implementation for python 2.2+?

-or-

2) will upgrading to a newer version of Python get me what I need?
[[ I looked thru the "New in 2.4" dox and nothing I needed was listed...]]

I've googled (off and on for more than a few days) and searched the 
archives, and came up empty so far...

This isn't life or death by any means... My laptop known for long battery 
life, so everything should be OK as is, but why grind the CPU if it's not 
necessary, eh? ;-) Just a niggling little thing that's been on the back of 
my mind...

[[ Oh, I found a reference to wxwindows that might have something that 
could work, but I was hoping for a little less overkill... my app barely 
uses ncurses. ]]

Thanks!
Roger "Merch" Merchberger

--
Roger "Merch" Merchberger  --  SysAdmin, Iceberg Computers
zmerch at 30below.com

Hi! I am a .signature virus.  Copy me into your .signature to join in!



More information about the Tutor mailing list