线程和睡眠? [英] threads and sleep?

查看:77
本文介绍了线程和睡眠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在编写多线程程序,我正在做什么

想知道是执行函数中的sleep命令会影响下面的

线程吗?这是我的意思的一个基本例子。


def main():

temp_var = True

而temp_var ==是的:

如果

t = threading.Thread(target = func1,args =" String")#note

这可能不起作用(问题的示例目的



t.start()

temp_var = t.isAlive()

else:

print"这一行应显示很多

sleep(2)


def func1(s ):

打印s


所以我想知道的问题是睡眠是否会暂停t $ / $
线程以及主要功能还是

主要功能睡眠的线程独立?


提前谢谢。

Hello all,

I am in the process of writing a multithreading program and what I was
wondering is a sleep command in an executing function will affect the
threads below it? Here is a basic example of what I mean.

def main():
temp_var = True
while temp_var == True:
if
t = threading.Thread( target = func1, args = "String") #note
this is probably non functional (example purposes for the question
only)
t.start()
temp_var = t.isAlive()
else:
print "This line should display a lot"
sleep(2)

def func1(s):
print s

so the question I was wondering is if the sleep will pause the t
thread as well as the main function or is the thread independat of the
main function sleep?

Thanks in advance.

推荐答案

Jeffrey Maitland写道:
Jeffrey Maitland wrote:
我正在编写一个多线程程序,而我想知道的是一个执行fu中的睡眠命令会影响下面的线程吗?


请注意执行功能。本身就是在一个线程中运行,

特别是主线程。其他线程不在下方。它在

任何特殊意义上,更像是旁边。 (或者,换句话说,

在Python中没有与线程相对位置的概念。)


这是一个基本的例子我的意思是。

[剪辑代码]所以我想知道的问题是,如果睡眠将暂停t />线程以及主要功能,或者是线程独立于
主要功能睡眠?
I am in the process of writing a multithreading program and what I was
wondering is a sleep command in an executing function will affect the
threads below it?
Note that the "executing function" is itself running inside a thread,
specifically the "main thread". The other threads are not "below" it in
any particular sense, more like beside it. (Or, to put it another way,
there is no such concept of relative position with threads in Python.)

Here is a basic example of what I mean.
[snip code] so the question I was wondering is if the sleep will pause the t
thread as well as the main function or is the thread independat of the
main function sleep?




您的代码已经非常接近工作了...为什么不运行它

和观察它是如何工作的?


无论如何,答案是不,time.sleep()只影响当前的

线程。 />

-Peter



Your code is very close to working already... why don''t you just run it
and observe how it works?

In any case, the answer is "no, time.sleep() affects only the current
thread".

-Peter


2005年7月4日星期一18:36:07 -0400,Peter Hansen< pe *** @ engcorp.com>

在comp.lang.python中声明了以下内容:
On Mon, 04 Jul 2005 18:36:07 -0400, Peter Hansen <pe***@engcorp.com>
declaimed the following in comp.lang.python:

无论如何,答案是不,时间 .sleep()只影响当前的
线程。

哎呀,这是获取的手段之一为了让其他线程/进程能够运行,允许其他线程/进程运行< G>

- ======= =========================================== ======= =====<
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG<
wu******@dm.net | Bestiaria支持人员<
========================================= ========= ============<
主页:< http://www.dm.net/~wulfraed/> <
溢出页面:< http://wlfraed.home.netcom.com/> <

In any case, the answer is "no, time.sleep() affects only the current
thread".
Heck, it is one of the means of getting number-crunchers to give
up the CPU to allow other threads/processes to run <G>

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <



大家好,


首先请关于我怀疑睡眠()部分的回复好像很多,但我并不是百分之百确定。


为了继续这个,我只想感谢任何有贡献的人

这个帖子(笑)。


好​​的,这里有。我遇到的问题是我有一个应用程序

(写/共同编写),其运行时长依赖于某些变量

传递给它(主要是精度变量,更准确的是,运行时间更长

- 这是有道理的)。然而,为了加快速度,我决定写一个程序的线程版试图加速

。我注意到的是,线程版本是

花费的时间可能更长。事情是在8 ia-64 proccessor系统上运行的线程

版本似乎

只使用2或3个porcessors,大约30%(fluxiates) 。我的猜测是

,6个线程正在运行,他们使用30%sprox,每个给定

CPUS。


什么我想做的就是说1个线程使用尽可能多的给定的

CPU并且如果新的线程被启动(添加),如果一个CPU

是可用它而不是使用相同的CPU。这样它应该会增加应用程序的速度。在这部分算法中,独立(非线程)应用程序使用90 +

%的单个cpu,这就是为什么我将b $ b拆分并线程化那里的算法试图加速它因为这个部分重复了几次。我可以发布

我正在做的通用代码,但由于它的保密性,不能发布实际代码

。预先再次感谢任何

和所有评论(甚至任何恶意的评论)


Jeff
Hello all,

First off Thanks for the responses on the sleep() part I suspected as
much but I wasn''t 100% sure.

To continue this I just want to pre thank anyone that contributes to
this thread(lol).

Ok here goes. The problem I have is I had an application
(wrote/co-wrote) that has a long run time dependant on some variables
passed to it (mainly accuracy variables, the more accurate the longer
the run time - makes sense). However in the hopes to speed it up I
decided to write a threaded version of the program to try and speed
it up. How ever what I am noticing is that the threaded version is
taking as long possibly longer to run. The thing is the threaded
version is running on an 8 ia-64 proccessor system and it seems to
only be using 2 or 3 porcessors at about 30% (fluxiates). My guess is
that 6 threads are running they are using 30% sprox each of a 2 given
CPUS.

What I would like to do is have say 1 thread use as much of a given
CPU as possible and if a new thread is started (added) that if a CPU
is available use it instead of using the same cpu. That way it should
speed the application up. The standalone (non-threaded) app uses 90+
% of a single cpu when in this part of the algorithm, that is why I
split and threaded that part of the algorithm there to try and speed
it up because this part is repeated several times. I can post
generic code of what I am doing but can''t post the actuall code
because of the confidentially of it. Thanks again in advance for any
and all comments (even any spitefull ones)

Jeff


这篇关于线程和睡眠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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