Spawing一个线程并打印点直到它完成 [英] Spawing a thread and printing dots until it finishes

查看:69
本文介绍了Spawing一个线程并打印点直到它完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一段代码来生成一个帖子并且

每半秒打印一次点,直到生成的线程结束。

代码是这样的



导入线程

类MyThread(threading.Thread):

def run(self):

myLongCommand()...


导入时间


t = MyThread( )

t.start()


而t.isAlive():

print"。"

time.sleep(.5)


打印确定


事情是这不打印每半秒一个点。只是

暂停了很长时间,直到线程结束并打印打印.OK。

但是如果我取出time.sleep(.5) "在线程完成之前它会保持打印点

非常快。所以它看起来像

time.sleep(.5)位不知何故搞乱了这个?


任何想法?


谢谢!

解决方案

2008年4月22日星期二07:10:07 -0700(PDT)

sophie_newbie< pa ********** @ gmail.comwrote:


导入线程

类MyThread (threading.Thread):

def run(self):

myLongCommand()......


导入时间


t = MyThread()

t.start()


而t.isAlive():

print"。"

time.sleep(.5)


打印OK


事情是这不会每半秒打印一个点。只是

暂停了很长时间,直到线程结束并打印打印.OK。

但是如果我取出time.sleep(.5) "在线程完成之前它会保持打印点

非常快。所以它看起来像

time.sleep(.5)位是以某种方式弄乱了这个?



我们知道你的主程序放弃了处理器,但没有完全定义MyThread,我们怎么知道它曾经做过?我b / b
怀疑它打了一次睡觉,如果有的话,然后进入最后的

打印声明。事实上,我怀疑这并不是你无论如何都要尝试的b $ b。此代码不会打印.OK。是否
进入循环。它可以打印出来;


..

OK


因为循环中的print语句会打印出来一个点上的一个点由

本身。


在寻找这些类型的答案时你应该真正尝试创建

a最小展示您正在质疑的行为的程序,然后将整个脚本剪切并粘贴到您未经编辑的消息中。经常

你甚至会在这个过程中回答你自己的问题。


-

D''Arcy JM Cain< ; da *** @ druid.net |民主是三只狼
http://www.druid.net/darcy/ |和一只羊投票

+1 416 425 1212(mod#0082)(eNTP)|什么是晚餐。


4月22日下午4:41,D''Arcy J.M. Cain < da ... @ druid.netwrote:


On Tue,2008年4月22日07:10:07 -0700(PDT)

>
sophie_newbie< paulgeele ... @ gmail.comwrote:


导入线程

类MyThread(threading.Thread):

def run(self):

myLongCommand()...


导入时间


t = MyThread()

t.start()


而t.isAlive():

print"。"

time.sleep(.5)


print" OK"


事情是这不会每半秒打印一个点。只是

暂停了很长时间,直到线程结束并打印打印.OK。

但是如果我取出time.sleep(.5) "在线程完成之前它会保持打印点

非常快。所以它看起来像

time.sleep(.5)位是以某种方式弄乱了这个?



我们知道您的主程序放弃了处理器,但没有完全定义MyThread,我们怎么知道它的确如此?我b / b
怀疑它打了一次睡觉,如果有的话,然后进入最后的

打印声明。事实上,我怀疑这并不是你无论如何都要尝试的b $ b。此代码不会打印.OK。是否
进入循环。它本来可以印这个;




OK


因为循环中的print语句会打印一个点缀在一条线上

本身。


在寻找这些类型的答案时,你应该真正尝试创建

a最小程序展示您正在质疑的行为,然后将整个脚本剪切并粘贴到您未经编辑的消息中。经常

你甚至会在这个过程中回答你自己的问题。


-

D''Arcy JM Cain< ; da ... @ druid.net |民主是三只狼,http://www.druid.net/darcy/ |和一只羊投票

+1 416 425 1212(mod#0082)(eNTP)|晚餐吃什么。



" myLongCommand()..."是通过Rpy调用R中的函数(统计

编程语言)(一个允许调用

R的python模块)。该调用需要几分钟才能执行。我正在尝试为这个R功能构建

a网络前端,而不是用户查看

a空白屏幕2-3分钟,我想打印点让他们觉得

喜欢这个程序并没有停止。


我所说的是没有time.sleep(.5) "一行,上面的

代码将在屏幕上连续打印2-3个小点,用一个荒谬的点数填充




然而使用time.sleep行,而不是在点之间停顿半秒

,它似乎打印出来,正如你正确指出的那样:

..

OK


暂停2-3分钟之间。好的。


我希望能稍微清理一下。我不是最不知道为什么

上面的代码不起作用,但希望有人有个主意。它不会与python无法同时处理多个

线程有关吗?我希望有一个解决方法。


2008年4月22日星期二09:32:38 -0700(PDT)

sophie_newbie< pa **********@gmail.com写道:


4月22日下午4:41,D''Arcy JM Cain" < da ... @ druid.netwrote:


我们知道您的主程序放弃了处理器但没有

完整定义MyThread我们怎么知道它曾经做过?我b / b
怀疑它打了一次睡觉,如果有的话,然后进入最后的

打印声明。事实上,我怀疑这并不是你无论如何都要尝试的b $ b。此代码不会打印.OK。是否
进入循环。它本来可以印这个;




OK


因为循环中的print语句会打印一个

本身就是一个点。


" myLongCommand()..."是通过Rpy调用R中的函数(统计

编程语言)(一个允许调用

R的python模块)。该调用需要几分钟才能执行。我正在尝试为这个R功能构建

a网络前端,而不是用户查看

a空白屏幕2-3分钟,我想打印点让他们觉得

喜欢这个程序并没有停止。


我所说的是没有time.sleep(.5) "一行,上面的

代码会在屏幕上连续打印2-3分钟的点数,用一个荒谬的点数填充




它能完成吗?在我看来,如果你从不放弃处理器,它会永远循环。请记住,线程不是进程。


而使用time.sleep行,而不是暂停半秒

点之间,似乎打印出来,正如你正确指出的那样:




OK


暂停2 3分钟之间。好的。



完全正确。它打印出第一个点,然后放弃处理器,以便你的另一个线程获得
。该线程运行2到3分钟然后

完成放弃处理器。那时你的睡眠已经睡了好几个.5秒,所以第一个线程可以自由运行。然后

检查循环的状态,看到它已完成并且在下一个打印OK的语句上继续
。并且退出。


我希望稍微澄清一下。我不是最不知道为什么

上面的代码不起作用,但希望有人有个主意。它不会与python无法同时处理多个

线程有关吗?我希望有一个解决方法。



我认为在理解这里发生的事情之前,你需要两件事来包裹你的头。

。首先,线程不是先发制人。除非你的线程放弃处理器,它将永远运行
。睡眠呼叫是放弃处理器的一种方式。


其次,睡眠()不会在给定时间到期后立即返回。

参数是它等待的最小时间量。在那之后

睡眠的线程被放回到运行队列中,现在是给予处理器的候选人
。你的另一个线程仍然需要

在它再次运行之前就放弃处理器,即使这样,你的队列之前的其他线程也可能是



那么,你的线程是否会放弃处理器而不是死亡?


-

D''Arcy JM该隐< da *** @ druid.net |民主是三只狼
http://www.druid.net/darcy/ |和一只羊投票

+1 416 425 1212(mod#0082)(eNTP)|什么是晚餐。


Hi, I''m trying to write a piece of code that spawns a thread and
prints dots every half second until the thread spawned is finished.
Code is
something like this:

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?

Any ideas?

Thanks!

解决方案

On Tue, 22 Apr 2008 07:10:07 -0700 (PDT)
sophie_newbie <pa**********@gmail.comwrote:

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***@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.


On Apr 22, 4:41 pm, "D''Arcy J.M. Cain" <da...@druid.netwrote:

On Tue, 22 Apr 2008 07:10:07 -0700 (PDT)

sophie_newbie<paulgeele...@gmail.comwrote:

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...@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.


On Tue, 22 Apr 2008 09:32:38 -0700 (PDT)
sophie_newbie <pa**********@gmail.comwrote:

On Apr 22, 4:41 pm, "D''Arcy J.M. Cain" <da...@druid.netwrote:

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 <da***@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.


这篇关于Spawing一个线程并打印点直到它完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆