Laughing at myself

Andy Jewell andy at wild-flower.co.uk
Mon May 12 17:20:15 EDT 2003


On Monday 12 May 2003 5:12 pm, Daniel Fackrell wrote:
> Just to make sure that everyone else can get some benefit from my
> self-induced pain:
>
> I have wasted a bit of time recently, wondering why Python doesn't offer a
> usleep() (sleep for microseconds) function.  I had found the time module,
> and used its time.sleep(), but was frustrated that it didn't have a
> time.usleep() as well.  After searching the web and the newsgroup archives,
> I finally figured I might get stuck implementing something like this at a
> low level.
>
> After all that, I was just reading a recent post reminding about the
> dangers of explicit type-checking reducing polymorphism, and somehow that
> reminded me of my problem.  I had been assuming (courtesy of my
> strongly-typed background) that time.sleep() took in integer argument, but
> in fact it takes any numerical argument, so that:
>
> time.sleep(0.000001)
>
> works just fine, and apparently perfectly.
>
> Thanks to Alex Martelli for being the one who set me a bit closer to the
> true path.


I thought /everyone/ knew that!  :-p

I did some testing ages ago, and found that the resolution is *very* dependant 
on your operating system, hardware, and system stress-level.  You need to 
remember that the requested number of seconds/fractions of a second are only 
guaranteed to be the *minimum* time the call spends 'sleeping'.  If the 
system you are running on is stressed, or just Windows 9x, it could very well 
take longer, by a very unpredictable margin.  If you need needle-point 
accuracy, you need to use a 'busy wait', but them it's probably time to start 
thinking in terms of something compiled like C/C++ if that's an issue for 
your program :-)

hth, -andyj





More information about the Python-list mailing list