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

Jim Dennis jimd at vega.starshine.org
Sat Apr 14 04:59:55 EDT 2001


In article <MOsw6.7387$8R5.921141 at typhoon.nyroc.rr.com>, Dan Maas wrote:
>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
	The Linux kernel uses a 10ms pre-emption (scheduling)
	interval.  So, every 10ms the Kernel gets a timer interrupt
	shifting each processor out of userspace into the
	interrupt handler which checks the scheduler, and the 
	run_queue and a few resource limits for the current process
	(processes on SMP), updates a few counters, etc.

	At that time the kernel determines whether it will give
	the current process on a given CPU another time slice, or
	perform a context switch to another process (or thread).

	Obviously you won't be able to sustain any processing
	at less then 10ms resolution for an sustained period 
	of time.  You could hack your kernel to lower its
	pre-emption interval (increasing the frequency and 
	increasing the kernel's overall overhead vs. userspace
	processing).  However, it seems like you'd prefer to have
	access some real-time features.

	Off hand I wouldn't know how Python interfaces to any
	of the several Linux real-time extensions and patches.
	There are the RTLinux (MURT like) patches from FSMLabs,
	the RTAI patches as used by Lineo, the Timesys patches,
	the KURT (Kansas Univ. Real-Time), and the Monta Vista
	configurable RTS (RT Sched) from Monta Vista.  Those are 
	just some of the Linux real-time alternatives (including
	hard, soft and hybrid features).

	Unfortunately these various approaches to RT under Linux
	are in fierce competition.  The represent a blurry herd
	of racing zebras to Guido, Larry, and Mr. Gosling:  which
	set of libraries and APIs should/could be implemented for
	use within Python, Perl, Java, etc?

	I certainly don't know the answer to that question.




More information about the Python-list mailing list