Spawing a thread and printing dots until it finishes

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Apr 22 13:26:01 EDT 2008


En Tue, 22 Apr 2008 13:32:38 -0300, sophie_newbie <paulgeeleher at gmail.com> escribió:
> On Apr 22, 4:41 pm, "D'Arcy J.M. Cain" <da... at druid.net> wrote:
>> On Tue, 22 Apr 2008 07:10:07 -0700 (PDT)
>> sophie_newbie<paulgeele... at gmail.com> wrote:
>> > import threading
>> > class MyThread ( threading.Thread ):
>> >         def run ( self ):
>> >                 myLongCommand()...
>>
>> > import time
>>
>> > t = MyThread()
>> > t.start()
>>
>> > while t.isAlive():
>> >         print "."
>> >         time.sleep(.5)
>>
>> > print "OK"
>>
>> > The thing is this doesn't print a dot every half second. It just
>> > pauses for ages until the thread is finished and prints prints ".OK".
>> > But if I take out the "time.sleep(.5)" line it will keep printing dots
>> > really fast until the thread is finished. So it looks like its the
>> > time.sleep(.5) bit that is messing this up somehow?
>>
>> 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
>
> "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.
>
> 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.

A possible explanation is that the RPy call does not release the GIL; once the main thread loses it due to sleep, it can never reacquire it again until the RPy call finishes.
Try contacting the RPy author, or perhaps there is a specific mailing list for RPy questions.

-- 
Gabriel Genellina




More information about the Python-list mailing list