getting a thread out of sleep

placid Bulkan at gmail.com
Wed Feb 21 17:47:50 EST 2007


On Feb 22, 3:23 am, mark <rkmr... at gmail.com> wrote:
> On 20 Feb 2007 21:26:18 -0800, placid <Bul... at gmail.com> wrote:
>
>
>
> > On Feb 21, 4:21 pm, "placid" <Bul... at gmail.com> wrote:
> > > On Feb 21, 4:12 pm, mark <rkmr... at gmail.com> wrote:
> > > > On 20 Feb 2007 20:47:57 -0800, placid <Bul... at gmail.com> wrote:
> > > > > 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
>
> > > > > > 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>
> > oops I've miss-typed the thread variable name the following should
> > work
>
> > <code>
> > if __name__ == "__main__":
> >     evtHandlerThread = eventhandler()
> >     evtHandlerThread.start()
>
> >     # do something here #
> >     evtHandlerThread.event.set()
>
> >     # do more stuff here #
> >     evtHandlerThread.event.set()
>
> > </code>
>
> Can I have the same thread process two or more events? Can you tell
> how to do this? The code you gave is waiting on one event right. How
> can I do it for more events?
> thanks a lot!
> mark

I don't think a thread can block on more than one event at a time. But
you can make it block on more then one event one at a time.

<code>

class eventhandler(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.events = [threading.Event(), threading.Event()]
        self.currentEvent = None
    def run:
        while True:
            for event in self.events:
                self.currentEvent = event
                # block until some event happens
                self.currentEvent.wait()
                """ do stuff here """
                self.currentEvent.clear()

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

     # do something here #
     evtHandlerThread.currentEvent.set()

     # do more stuff here #
     evtHandlerThread.currentEvent.set()

</code>

what the thread does is sequentially waits for two events to happen
and then execute the same code. You could change this code to perform
different functions for different event objects.

Cheers




More information about the Python-list mailing list