getting a thread out of sleep

placid Bulkan at gmail.com
Tue Feb 20 23:47:57 EST 2007


On Feb 21, 3:08 pm, mark <rkmr... at gmail.com> wrote:
> 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








More information about the Python-list mailing list