[Tutor] Re: how do I sleep?

dman dsh8290@rit.edu
Tue, 27 Nov 2001 15:24:10 -0500


On Tue, Nov 27, 2001 at 08:34:35PM +0000, Pablo Prieto wrote:
| Hello!
| 
| I'm Pablo, from Spain.

Hi Pablo.  Just a tip : when posting a question start by composing a
new message with a relevant Subject: line, rather than replying to an
unrelated message.

| How can I give time-slices of processing to the OS when I'm stuck in a very
| cost-time loop?. I mean, not to freeze the system (NT, you know) while the
| loop is on.

What you are looking for is the sleep() function in the time module.
So you would have :

import time
# a long loop
while 1 :
    print "this takes lots of time ;-)"
    time.sleep( 5 )  # 5 seconds, approximately


The disadvantage of doing this is that your long loop now takes a lot
more time (as measured by a clock on the wall).  I don't know about
NT, but a decent kernel will perform the time-slicing between
processes on its own.  For example, if on my linux box I make an
infinite loop (say  'while 1 : pass'), my CPU will be maxed out.  Now
if I try and do something else in another process, that infinite loop
in python only gets part of the CPU and my other stuff gets enough CPU
to run with very little noticeable degradation of performance.

HTH,
-D

-- 

(E)scape (M)eta (A)lt (C)ontrol (S)hift