让线程失去理智 [英] getting a thread out of sleep

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

问题描述

现在我有一个线程可以睡一段时间并检查是否发生了

事件并重新入睡。现在我想要线程

睡觉直到事件发生处理事件并返回到

睡眠。怎么做?

谢谢

mark

class eventhndler(threading.Thread):

def __init __(自我):

threading.Thread .__ init __(self)


def run(self):

而True:

time.sleep(SLEEPTIME)

''''''做事件'''''

解决方案

2月21日下午3:08,标记< rkmr ... @ gmail.comwrote:


现在我有一个睡眠一段时间的线程并检查是否发生了

事件并重新入睡。现在我想要线程

睡觉直到事件发生处理事件并返回到

睡眠。怎么做?

谢谢

mark


class eventhndler(threading.Thread):

def __init __(self):

threading.Thread .__ init __(self)


def run(self):

while是的:

time.sleep(SLEEPTIME)

'''''''做事件''''''



我这样做的方法是使用threading.Event(
http://docs.python.org/lib/event-objects.html


< code>


class eventhandler(threading.Thread):

def __init __(self):

threading.Thread .__ init __(self)

self.event = threading.Event()

def run:

而True:

#block直到某些事件发生

self.event.wait()

"""在这里做什么""

self.event.clear()


< / code>


使用它的方法是让主/单独的线程设置()

事件对象。

干杯


2007年2月20日20:47:57 -0800,placid< Bu **** @ gmail.comwrote:


2月21日下午3:08,标记< rkmr ... @ gmail.comwrote:


现在我有一个线程可以睡一段时间并检查如果

事件发生并重新入睡。现在反而我希望线程

睡觉直到事件发生并处理事件并重新进入睡眠状态


class eventhndler(threading.Thread):

def __init __(自我):

threading.Thread .__ init __(self)

def run(self):

而True:

time.sleep(SLEEPTIME)

'''''''做事件''''''


我这样做的方法是使用threading.Event(
http://docs.python.org/lib/event-objects.html


< code>


class eventhandler(threading.Thread):

def __init __(self):

threading.Thread .__ init __(self)

self.event = threading.Event()

def run:

而True:

#阻止直到我发生的事件

self.event.wait()

"""在这里做的事情"""

self.event.clear()

< / code>


使用它的方法是让主/单独的线程设置()

事件对象。



你能给出一个如何让主三元组设置事件对象的例子吗?

这正是我想做的事情!

非常感谢!

mark


2月21日下午4:12,标记< rkmr ... @ gmail.comwrote:


2007年2月20日20:47:57 -0800,placid< Bul ... @ gmail.comwrote:


2月21日下午3:08,标记< rkmr ... @ gmail.comwrote:


现在我有一个线程可以休眠一段时间并检查是否发生了

事件并重新入睡。现在我希望线程

睡觉直到事件发生并处理事件并返回睡眠

< blockquote class =post_quotes>
class eventhndler(threading.Thread):

def __init __(self):

threading.Thread .__ init __(self)

def run(self):

而True:

time.sleep(SLEEPTIME)

'''' ''''做事件''''''


我这样做的方法是使用threading.Event (
http://docs.python.org/lib /event-objects.html


< code>


class eventhandler(threading.Thread):

def __init __(self):

threading.Thread .__ init __(self)

self.event = threading.Event()

def run:

而True:

#阻止直到某些事件发生

self.event.wait()

"""在这里做的事情"""

self.event.clear()

< / code>


使用它的方法是让主/单独的线程设置()

事件对象。



你能给出一个如何让主三元组设置事件对象的例子吗?

这正是我想做的事情!

非常感谢!

mark



设置事件对象

< code>


if __name__ ==" __ main __":

evtHandlerThread = eventhandler()

evtHandler.start()


#在这里做点什么#

evtHandler.event.set()


#在这里做更多的事情#

evtHandler.event.set()


< / code>


希望这就是你想要的。

干杯


Right now I have a thread that sleeps for sometime and check if an
event has happened and go back to sleep. Now instead I want the thread
to sleep until the event has occured process the event and go back to
sleep. How to do this?
thanks
mark
class eventhndler(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)

def run(self):
while True:
time.sleep(SLEEPTIME)
''''''''do event stuff''''''

解决方案

On Feb 21, 3:08 pm, mark <rkmr...@gmail.comwrote:

Right now I have a thread that sleeps for sometime and check if an
event has happened and go back to sleep. Now instead I want the thread
to sleep until the event has occured process the event and go back to
sleep. How to do this?
thanks
mark

class eventhndler(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)

def run(self):
while True:
time.sleep(SLEEPTIME)
''''''''do event stuff''''''

The way i would do this is by using an threading.Event (
http://docs.python.org/lib/event-objects.html )

<code>

class eventhandler(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.event = threading.Event()

def run:
while True:
# block until some event happens
self.event.wait()
""" do stuff here """
self.event.clear()

</code>

the way to use this is to get the main/separate thread to set() the
event object.
Cheers


On 20 Feb 2007 20:47:57 -0800, placid <Bu****@gmail.comwrote:

On Feb 21, 3:08 pm, mark <rkmr...@gmail.comwrote:

Right now I have a thread that sleeps for sometime and check if an
event has happened and go back to sleep. Now instead I want the thread
to sleep until the event has occured process the event and go back to sleep

class eventhndler(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
while True:
time.sleep(SLEEPTIME)
''''''''do event stuff''''''


The way i would do this is by using an threading.Event (
http://docs.python.org/lib/event-objects.html )

<code>

class eventhandler(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.event = threading.Event()
def run:
while True:
# block until some event happens
self.event.wait()
""" do stuff here """
self.event.clear()
</code>

the way to use this is to get the main/separate thread to set() the
event object.

Can you give an example of how to get the main threead to set teh event object?
this is exactly what i wanted to do!
thanks a lot!
mark


On Feb 21, 4:12 pm, mark <rkmr...@gmail.comwrote:

On 20 Feb 2007 20:47:57 -0800, placid <Bul...@gmail.comwrote:

On Feb 21, 3:08 pm, mark <rkmr...@gmail.comwrote:

Right now I have a thread that sleeps for sometime and check if an
event has happened and go back to sleep. Now instead I want the thread
to sleep until the event has occured process the event and go back to sleep

class eventhndler(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
while True:
time.sleep(SLEEPTIME)
''''''''do event stuff''''''

The way i would do this is by using an threading.Event (
http://docs.python.org/lib/event-objects.html)

<code>

class eventhandler(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.event = threading.Event()
def run:
while True:
# block until some event happens
self.event.wait()
""" do stuff here """
self.event.clear()
</code>

the way to use this is to get the main/separate thread to set() the
event object.


Can you give an example of how to get the main threead to set teh event object?
this is exactly what i wanted to do!
thanks a lot!
mark

To set the event object

<code>

if __name__ == "__main__":
evtHandlerThread = eventhandler()
evtHandler.start()

# do something here #
evtHandler.event.set()

# do more stuff here #
evtHandler.event.set()

</code>

Hope thats what your looking for.
Cheers


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

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