Spawing a thread and printing dots until it finishes
Diez B. Roggisch
deets at nospam.web.de
Tue Apr 22 15:34:24 EDT 2008
Larry Bates schrieb:
> sophie_newbie wrote:
>> 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
>>> 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.
>>>
>>> When looking for these sorts of answers you should really try to create
>>> a smallest program that exhibits the behaviour you are questioning and
>>> then cut and paste the entire script into your message unedited. Often
>>> enough you will even answer your own question in the process.
>>>
>>> --
>>> D'Arcy J.M. Cain <da... at druid.net> | Democracy is three
>>> wolveshttp://www.druid.net/darcy/ | and a sheep voting on
>>> +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
>>
>> "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.
>>
>> 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.
>
> For a web front end you wouldn't go this route at all. You would get a
> progressive .GIF file that gets loaded into the client's browser and shows
> "activity" while the server does its thing. When the browser refreshes
> (after the server application completes) it would go away. You can't
> update a client's browser by writing dots to anything.
Yes and no. You are right of course that the dot-thread is not working
that way. But if you replace the dot-thread with the
http-request-thread, the question remains: why is it blocking? Your
approach doesn't tackle that.
If nothing else helps, a subprocess must be spawned.
Diez
More information about the Python-list
mailing list