Setting sleep interval less than 10 mS on a Linux PC?

Dan Maas dmaas at nospam.dcine.com
Wed Mar 28 23:35:40 CEST 2001


In article <3AC22D31.FD4644B1 at spammyraqia.com>, "David Lees"
<DavidlNo at spammyraqia.com> wrote:

> I suspect this is a system question, but I will ask anyway.  I am using
> the Python sleep method (time.sleep) and on a PC running RedHat 7.0
> Linux,  it has a 10 millisecond lower limit.  Below that it still wakes
> up every 10 milliseconds, regardless of the argument.

This is a nasty problem - time.sleep() uses select() internally, and on
Linux select() cannot sleep for less than 10ms.

Easy way out - just use a busy loop:

start_time = time.time()
while (time.time() - start) < 0.001:
	pass

time.time() has microsecond accuracy so you will get very close to the
desired delay. Of course this burns CPU time, but with a 1ms polling
interval I assume you're not too worried about that.

Hard way out: find or write a python module that uses nanosleep()
internally; I *think* nanosleep() can give you <10ms delays without
busy-waiting. (you may need a kernel patch though :[...)

Regards,
Dan



More information about the Python-list mailing list