Spawing a thread and printing dots until it finishes

D'Arcy J.M. Cain darcy at druid.net
Tue Apr 22 13:25:07 EDT 2008


On Tue, 22 Apr 2008 09:32:38 -0700 (PDT)
sophie_newbie <paulgeeleher at gmail.com> wrote:
> On Apr 22, 4:41 pm, "D'Arcy J.M. Cain" <da... at druid.net> wrote:
> > We know that your main routine gives up the processor but without a
> > full definition of MyThread how do we know that it ever does?  I
> > suspect that it hits sleep once, if at all, and then goes to the final
> > print statement.  In fact, I suspect that this is not exactly what you
> > tried anyway.  This code would not have printed ".OK" whether it
> > entered the loop or not.  It could have printed this;
> >
> > .
> > OK
> >
> > because the print statement in the loop will print a dot on a line by
> > itself.

> "myLongCommand()... " is a call to an function in R (the statistical
> programming language) via Rpy (A python module that allows calls to
> R). The call takes a couple of minutes to execute. I'm trying to build
> a web front end to this R function and instead of the user looking at
> a blank screen for 2-3 mins, I want to print dots to let them feel
> like the program isn't hanging.
> 
> What I am saying is that without the "time.sleep(.5)" line, the above
> code will print dots on the screen continuously for 2-3 mins, filling
> it up with a ridiculous ammount of dots.

Does it ever finish?  It seems to me that it would loop forever if you
never give up the processor.  Remember, threads are not processes.

> Whereas with the time.sleep line, instead of pausing for half a second
> between dots, its seems to print, as you correctly pointed out:
> 
> .
> OK
> 
> With a pause of 2-3 minutes between the . and the OK.

Exactly.  It prints the first dot and then gives up the processor to
your other thread.  That thread runs for 2 to 3 minutes and then
completes giving up the processor.  At that point your sleep has slept
for at least .5 seconds so the first thread if free to run.  It then
checks the condition of the loop, sees that it is finished and
continues on the the next statement which prints the "OK" and exits.

> I hope that clears things up a little. I haven't the faintest idea why
> the code above doesn't work but hope someone has an idea. It wouldn't
> be something to do with python not being able to handle multiple
> threads at the same time or something? I hope there is a workaround.

I think that there are two things that you need to wrap your head
around before understanding what is happening here.  First, threads are
NOT pre-emptive.  Unless your thread gives up the processor it will run
forever.  The sleep call is one way to give up the processor.

Second, sleep() does not return as soon as the time given has expired.
The argument is the MINIMUM amount of time that it waits.  After that
the thread that slept is put back onto the run queue and is now a
candidate to be given the processor.  Your other thread still has to
give up the processor before it can run again and even then there may
be other threads on the queue ahead of yours.

So, does your thread ever give up the processor other than by dying?

-- 
D'Arcy J.M. Cain <darcy at druid.net>         |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.



More information about the Python-list mailing list