getting a thread out of sleep

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mark

    #1

    getting a thread out of sleep

    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(thr eading.Thread):
    def __init__(self):
    threading.Threa d.__init__(self )

    def run(self):
    while True:
    time.sleep(SLEE PTIME)
    ''''do event stuff'''
  • placid

    #2
    Re: getting a thread out of sleep

    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(thr eading.Thread):
    def __init__(self):
    threading.Threa d.__init__(self )
    >
    def run(self):
    while True:
    time.sleep(SLEE PTIME)
    ''''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(th reading.Thread) :
    def __init__(self):
    threading.Threa d.__init__(self )
    self.event = threading.Event ()

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

    </code>

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


    Cheers





    Comment

    • mark

      #3
      Re: getting a thread out of sleep

      On 20 Feb 2007 20:47:57 -0800, placid <Bulkan@gmail.c omwrote:
      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(thr eading.Thread):
      def __init__(self):
      threading.Threa d.__init__(self )
      def run(self):
      while True:
      time.sleep(SLEE PTIME)
      ''''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(th reading.Thread) :
      def __init__(self):
      threading.Threa d.__init__(self )
      self.event = threading.Event ()
      def run:
      while True:
      # block until some event happens
      self.event.wait ()
      """ do stuff here """
      self.event.clea r()
      </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

      Comment

      • placid

        #4
        Re: getting a thread out of sleep

        On Feb 21, 4:12 pm, mark <rkmr...@gmail. comwrote:
        On 20 Feb 2007 20:47:57 -0800, placid <Bul...@gmail.c omwrote:
        >
        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(thr eading.Thread):
        def __init__(self):
        threading.Threa d.__init__(self )
        def run(self):
        while True:
        time.sleep(SLEE PTIME)
        ''''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(th reading.Thread) :
        def __init__(self):
        threading.Threa d.__init__(self )
        self.event = threading.Event ()
        def run:
        while True:
        # block until some event happens
        self.event.wait ()
        """ do stuff here """
        self.event.clea r()
        </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__":
        evtHandlerThrea d = eventhandler()
        evtHandler.star t()

        # do something here #
        evtHandler.even t.set()

        # do more stuff here #
        evtHandler.even t.set()

        </code>

        Hope thats what your looking for.


        Cheers


        Comment

        • placid

          #5
          Re: getting a thread out of sleep

          On Feb 21, 4:21 pm, "placid" <Bul...@gmail.c omwrote:
          On Feb 21, 4:12 pm, mark <rkmr...@gmail. comwrote:
          >
          >
          >
          On 20 Feb 2007 20:47:57 -0800, placid <Bul...@gmail.c omwrote:
          >
          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(thr eading.Thread):
          def __init__(self):
          threading.Threa d.__init__(self )
          def run(self):
          while True:
          time.sleep(SLEE PTIME)
          ''''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(th reading.Thread) :
          def __init__(self):
          threading.Threa d.__init__(self )
          self.event = threading.Event ()
          def run:
          while True:
          # block until some event happens
          self.event.wait ()
          """ do stuff here """
          self.event.clea r()
          </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__":
          evtHandlerThrea d = eventhandler()
          evtHandler.star t()
          >
          # do something here #
          evtHandler.even t.set()
          >
          # do more stuff here #
          evtHandler.even t.set()
          >
          </code>
          >
          Hope thats what your looking for.
          >
          Cheers
          oops I've miss-typed the thread variable name the following should
          work

          <code>

          if __name__ == "__main__":
          evtHandlerThrea d = eventhandler()
          evtHandlerThrea d.start()

          # do something here #
          evtHandlerThrea d.event.set()

          # do more stuff here #
          evtHandlerThrea d.event.set()

          </code>

          Comment

          • mark

            #6
            Re: getting a thread out of sleep

            On 20 Feb 2007 21:26:18 -0800, placid <Bulkan@gmail.c omwrote:
            On Feb 21, 4:21 pm, "placid" <Bul...@gmail.c omwrote:
            On Feb 21, 4:12 pm, mark <rkmr...@gmail. comwrote:
            On 20 Feb 2007 20:47:57 -0800, placid <Bul...@gmail.c omwrote:
            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(thr eading.Thread):
            def __init__(self):
            threading.Threa d.__init__(self )
            def run(self):
            while True:
            time.sleep(SLEE PTIME)
            ''''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(th reading.Thread) :
            def __init__(self):
            threading.Threa d.__init__(self )
            self.event = threading.Event ()
            def run:
            while True:
            # block until some event happens
            self.event.wait ()
            """ do stuff here """
            self.event.clea r()
            </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__":
            evtHandlerThrea d = eventhandler()
            evtHandlerThrea d.start()
            >
            # do something here #
            evtHandlerThrea d.event.set()
            >
            # do more stuff here #
            evtHandlerThrea d.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

            Comment

            • placid

              #7
              Re: getting a thread out of sleep

              On Feb 22, 3:23 am, mark <rkmr...@gmail. comwrote:
              On 20 Feb 2007 21:26:18 -0800, placid <Bul...@gmail.c omwrote:
              >
              >
              >
              On Feb 21, 4:21 pm, "placid" <Bul...@gmail.c omwrote:
              On Feb 21, 4:12 pm, mark <rkmr...@gmail. comwrote:
              On 20 Feb 2007 20:47:57 -0800, placid <Bul...@gmail.c omwrote:
              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(thr eading.Thread):
              def __init__(self):
              threading.Threa d.__init__(self )
              def run(self):
              while True:
              time.sleep(SLEE PTIME)
              ''''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(th reading.Thread) :
              def __init__(self):
              threading.Threa d.__init__(self )
              self.event = threading.Event ()
              def run:
              while True:
              # block until some event happens
              self.event.wait ()
              """ do stuff here """
              self.event.clea r()
              </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__":
              evtHandlerThrea d = eventhandler()
              evtHandlerThrea d.start()
              >
              # do something here #
              evtHandlerThrea d.event.set()
              >
              # do more stuff here #
              evtHandlerThrea d.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(th reading.Thread) :
              def __init__(self):
              threading.Threa d.__init__(self )
              self.events = [threading.Event (), threading.Event ()]
              self.currentEve nt = None
              def run:
              while True:
              for event in self.events:
              self.currentEve nt = event
              # block until some event happens
              self.currentEve nt.wait()
              """ do stuff here """
              self.currentEve nt.clear()

              if __name__ == "__main__":
              evtHandlerThrea d = eventhandler()
              evtHandlerThrea d.start()

              # do something here #
              evtHandlerThrea d.currentEvent. set()

              # do more stuff here #
              evtHandlerThrea d.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

              Comment

              • mark

                #8
                Re: getting a thread out of sleep

                On 21 Feb 2007 14:47:50 -0800, placid <Bulkan@gmail.c omwrote:
                On Feb 22, 3:23 am, mark <rkmr...@gmail. comwrote:
                On 20 Feb 2007 21:26:18 -0800, placid <Bul...@gmail.c omwrote:


                On Feb 21, 4:21 pm, "placid" <Bul...@gmail.c omwrote:
                On Feb 21, 4:12 pm, mark <rkmr...@gmail. comwrote:
                On 20 Feb 2007 20:47:57 -0800, placid <Bul...@gmail.c omwrote:
                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(thr eading.Thread):
                def __init__(self):
                threading.Threa d.__init__(self )
                def run(self):
                while True:
                time.sleep(SLEE PTIME)
                ''''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(th reading.Thread) :
                def __init__(self):
                threading.Threa d.__init__(self )
                self.event = threading.Event ()
                def run:
                while True:
                # block until some event happens
                self.event.wait ()
                """ do stuff here """
                self.event.clea r()
                </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__":
                evtHandlerThrea d = eventhandler()
                evtHandlerThrea d.start()
                # do something here #
                evtHandlerThrea d.event.set()
                # do more stuff here #
                evtHandlerThrea d.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(th reading.Thread) :
                def __init__(self):
                threading.Threa d.__init__(self )
                self.events = [threading.Event (), threading.Event ()]
                self.currentEve nt = None
                def run:
                while True:
                for event in self.events:
                self.currentEve nt = event
                # block until some event happens
                self.currentEve nt.wait()
                """ do stuff here """
                self.currentEve nt.clear()
                >
                if __name__ == "__main__":
                evtHandlerThrea d = eventhandler()
                evtHandlerThrea d.start()
                >
                # do something here #
                evtHandlerThrea d.currentEvent. set()
                >
                # do more stuff here #
                evtHandlerThrea d.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.
                Once the thread starts it is going to wait on the event that is the
                first element of the list right? This would mean :
                evtHandlerThrea d.currentEvent. set(): that I have only one event right?
                Can you explain how I can have different event objects. I dont see how
                I can do different functinos for same event.

                Thanks a lot!

                mark

                Comment

                • placid

                  #9
                  Re: getting a thread out of sleep

                  On Feb 22, 10:20 am, mark <rkmr...@gmail. comwrote:
                  On 21 Feb 2007 14:47:50 -0800, placid <Bul...@gmail.c omwrote:
                  >
                  >
                  >
                  On Feb 22, 3:23 am, mark <rkmr...@gmail. comwrote:
                  On 20 Feb 2007 21:26:18 -0800, placid <Bul...@gmail.c omwrote:
                  >
                  On Feb 21, 4:21 pm, "placid" <Bul...@gmail.c omwrote:
                  On Feb 21, 4:12 pm, mark <rkmr...@gmail. comwrote:
                  On 20 Feb 2007 20:47:57 -0800, placid <Bul...@gmail.c omwrote:
                  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(thr eading.Thread):
                  def __init__(self):
                  threading.Threa d.__init__(self )
                  def run(self):
                  while True:
                  time.sleep(SLEE PTIME)
                  ''''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(th reading.Thread) :
                  def __init__(self):
                  threading.Threa d.__init__(self )
                  self.event = threading.Event ()
                  def run:
                  while True:
                  # block until some event happens
                  self.event.wait ()
                  """ do stuff here """
                  self.event.clea r()
                  </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__":
                  evtHandlerThrea d = eventhandler()
                  evtHandlerThrea d.start()
                  >
                  # do something here #
                  evtHandlerThrea d.event.set()
                  >
                  # do more stuff here #
                  evtHandlerThrea d.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(th reading.Thread) :
                  def __init__(self):
                  threading.Threa d.__init__(self )
                  self.events = [threading.Event (), threading.Event ()]
                  self.currentEve nt = None
                  def run:
                  while True:
                  for event in self.events:
                  self.currentEve nt = event
                  # block until some event happens
                  self.currentEve nt.wait()
                  """ do stuff here """
                  self.currentEve nt.clear()
                  >
                  if __name__ == "__main__":
                  evtHandlerThrea d = eventhandler()
                  evtHandlerThrea d.start()
                  >
                  # do something here #
                  evtHandlerThrea d.currentEvent. set()
                  >
                  # do more stuff here #
                  evtHandlerThrea d.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.
                  >
                  Once the thread starts it is going to wait on the event that is the
                  first element of the list right? This would mean :
                  This is correct.
                  evtHandlerThrea d.currentEvent. set(): that I have only one event right?
                  this means that the current event occurred.
                  Can you explain how I can have different event objects. I dont see how
                  I can do different functinos for same event.
                  >
                  Thanks a lot!
                  >
                  mark
                  To run different functions for the same event is easy, just write a
                  wrapper class around threading.event () and include some method that
                  you will run and assign this to different functions for each
                  EventWrapper.

                  <code>

                  class EventWrapper():
                  def __init__(self,w ork ):
                  self.event = threading.Event ()
                  self.eventWork = work

                  def wait(self):
                  self.event.wait ()

                  def clear(self)
                  self.event.clea r()

                  def eventWork(self) :
                  print "no work"

                  class eventhandler(th reading.Thread) :
                  def __init__(self, events = None):
                  threading.Threa d.__init__(self )
                  self.events = events
                  self.currentEve nt = None
                  def run:
                  while True:
                  if self.events:
                  for event in self.events:
                  self.currentEve nt = event
                  # block until the current event happens
                  self.currentEve nt.wait()
                  self.currentEve nt.eventWork()
                  self.currentEve nt.clear()

                  def eventOneWork():
                  # do some event 1 specific work here

                  def eventTwoWork():
                  # do some event 2 specific work here

                  if __name__ == "__main__":
                  events = [EventWrapper(ev entOneWork),Eve ntWrapper(event TwoWork)]

                  evtHandlerThrea d = eventhandler(ev ents)
                  evtHandlerThrea d.start()

                  # do something here #
                  evtHandlerThrea d.currentEvent. set()
                  # do more stuff here #
                  evtHandlerThrea d.currentEvent. set()

                  </code>

                  So you have a EventWrapper class that now contains the Event object
                  and a workEvent() method which is assigned to a function you create.

                  Hope this helps.

                  Cheers

                  Comment

                  • mark

                    #10
                    Re: getting a thread out of sleep

                    On 21 Feb 2007 16:10:51 -0800, placid <Bulkan@gmail.c omwrote:
                    On Feb 22, 10:20 am, mark <rkmr...@gmail. comwrote:
                    On 21 Feb 2007 14:47:50 -0800, placid <Bul...@gmail.c omwrote:


                    On Feb 22, 3:23 am, mark <rkmr...@gmail. comwrote:
                    On 20 Feb 2007 21:26:18 -0800, placid <Bul...@gmail.c omwrote:
                    On Feb 21, 4:21 pm, "placid" <Bul...@gmail.c omwrote:
                    On Feb 21, 4:12 pm, mark <rkmr...@gmail. comwrote:
                    On 20 Feb 2007 20:47:57 -0800, placid <Bul...@gmail.c omwrote:
                    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(thr eading.Thread):
                    def __init__(self):
                    threading.Threa d.__init__(self )
                    def run(self):
                    while True:
                    time.sleep(SLEE PTIME)
                    ''''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(th reading.Thread) :
                    def __init__(self):
                    threading.Threa d.__init__(self )
                    self.event = threading.Event ()
                    def run:
                    while True:
                    # block until some event happens
                    self.event.wait ()
                    """ do stuff here """
                    self.event.clea r()
                    </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__":
                    evtHandlerThrea d = eventhandler()
                    evtHandlerThrea d.start()
                    # do something here #
                    evtHandlerThrea d.event.set()
                    # do more stuff here #
                    evtHandlerThrea d.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(th reading.Thread) :
                    def __init__(self):
                    threading.Threa d.__init__(self )
                    self.events = [threading.Event (), threading.Event ()]
                    self.currentEve nt = None
                    def run:
                    while True:
                    for event in self.events:
                    self.currentEve nt = event
                    # block until some event happens
                    self.currentEve nt.wait()
                    """ do stuff here """
                    self.currentEve nt.clear()
                    if __name__ == "__main__":
                    evtHandlerThrea d = eventhandler()
                    evtHandlerThrea d.start()
                    # do something here #
                    evtHandlerThrea d.currentEvent. set()
                    # do more stuff here #
                    evtHandlerThrea d.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.
                    Once the thread starts it is going to wait on the event that is the
                    first element of the list right? This would mean :
                    >
                    This is correct.
                    >
                    evtHandlerThrea d.currentEvent. set(): that I have only one event right?
                    >
                    this means that the current event occurred.
                    >
                    Can you explain how I can have different event objects. I dont see how
                    I can do different functinos for same event.

                    Thanks a lot!

                    mark
                    >
                    To run different functions for the same event is easy, just write a
                    wrapper class around threading.event () and include some method that
                    you will run and assign this to different functions for each
                    EventWrapper.
                    >
                    <code>
                    >
                    class EventWrapper():
                    def __init__(self,w ork ):
                    self.event = threading.Event ()
                    self.eventWork = work
                    >
                    def wait(self):
                    self.event.wait ()
                    >
                    def clear(self)
                    self.event.clea r()
                    >
                    def eventWork(self) :
                    print "no work"
                    >
                    class eventhandler(th reading.Thread) :
                    def __init__(self, events = None):
                    threading.Threa d.__init__(self )
                    self.events = events
                    self.currentEve nt = None
                    def run:
                    while True:
                    if self.events:
                    for event in self.events:
                    self.currentEve nt = event
                    # block until the current event happens
                    self.currentEve nt.wait()
                    self.currentEve nt.eventWork()
                    self.currentEve nt.clear()
                    >
                    def eventOneWork():
                    # do some event 1 specific work here
                    >
                    def eventTwoWork():
                    # do some event 2 specific work here
                    >
                    if __name__ == "__main__":
                    events = [EventWrapper(ev entOneWork),Eve ntWrapper(event TwoWork)]
                    >
                    evtHandlerThrea d = eventhandler(ev ents)
                    evtHandlerThrea d.start()
                    >
                    # do something here #
                    evtHandlerThrea d.currentEvent. set()
                    # do more stuff here #
                    evtHandlerThrea d.currentEvent. set()
                    >
                    </code>
                    >
                    So you have a EventWrapper class that now contains the Event object
                    and a workEvent() method which is assigned to a function you create.
                    >
                    THanks a lot! Does this have to have event1 and event2 occur in
                    sequence? Will this still work even if only event2 occurs and event1
                    never occurs?
                    thanks
                    mark

                    Comment

                    • placid

                      #11
                      Re: getting a thread out of sleep

                      On Feb 22, 12:08 pm, mark <rkmr...@gmail. comwrote:
                      On 21 Feb 2007 16:10:51 -0800, placid <Bul...@gmail.c omwrote:
                      >
                      On Feb 22, 10:20 am, mark <rkmr...@gmail. comwrote:
                      On 21 Feb 2007 14:47:50 -0800, placid <Bul...@gmail.c omwrote:
                      >
                      On Feb 22, 3:23 am, mark <rkmr...@gmail. comwrote:
                      On 20 Feb 2007 21:26:18 -0800, placid <Bul...@gmail.c omwrote:
                      >
                      On Feb 21, 4:21 pm, "placid" <Bul...@gmail.c omwrote:
                      On Feb 21, 4:12 pm, mark <rkmr...@gmail. comwrote:
                      On 20 Feb 2007 20:47:57 -0800, placid <Bul...@gmail.c omwrote:
                      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(thr eading.Thread):
                      def __init__(self):
                      threading.Threa d.__init__(self )
                      def run(self):
                      while True:
                      time.sleep(SLEE PTIME)
                      ''''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(th reading.Thread) :
                      def __init__(self):
                      threading.Threa d.__init__(self )
                      self.event = threading.Event ()
                      def run:
                      while True:
                      # block until some event happens
                      self.event.wait ()
                      """ do stuff here """
                      self.event.clea r()
                      </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__":
                      evtHandlerThrea d = eventhandler()
                      evtHandlerThrea d.start()
                      >
                      # do something here #
                      evtHandlerThrea d.event.set()
                      >
                      # do more stuff here #
                      evtHandlerThrea d.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(th reading.Thread) :
                      def __init__(self):
                      threading.Threa d.__init__(self )
                      self.events = [threading.Event (), threading.Event ()]
                      self.currentEve nt = None
                      def run:
                      while True:
                      for event in self.events:
                      self.currentEve nt = event
                      # block until some event happens
                      self.currentEve nt.wait()
                      """ do stuff here """
                      self.currentEve nt.clear()
                      >
                      if __name__ == "__main__":
                      evtHandlerThrea d = eventhandler()
                      evtHandlerThrea d.start()
                      >
                      # do something here #
                      evtHandlerThrea d.currentEvent. set()
                      >
                      # do more stuff here #
                      evtHandlerThrea d.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.
                      >
                      Once the thread starts it is going to wait on the event that is the
                      first element of the list right? This would mean :
                      >
                      This is correct.
                      >
                      evtHandlerThrea d.currentEvent. set(): that I have only one event right?
                      >
                      this means that the current event occurred.
                      >
                      Can you explain how I can have different event objects. I dont see how
                      I can do different functinos for same event.
                      >
                      Thanks a lot!
                      >
                      mark
                      >
                      To run different functions for the same event is easy, just write a
                      wrapper class around threading.event () and include some method that
                      you will run and assign this to different functions for each
                      EventWrapper.
                      >
                      <code>
                      >
                      class EventWrapper():
                      def __init__(self,w ork ):
                      self.event = threading.Event ()
                      self.eventWork = work
                      >
                      def wait(self):
                      self.event.wait ()
                      >
                      def clear(self)
                      self.event.clea r()
                      >
                      def eventWork(self) :
                      print "no work"
                      >
                      class eventhandler(th reading.Thread) :
                      def __init__(self, events = None):
                      threading.Threa d.__init__(self )
                      self.events = events
                      self.currentEve nt = None
                      def run:
                      while True:
                      if self.events:
                      for event in self.events:
                      self.currentEve nt = event
                      # block until the current event happens
                      self.currentEve nt.wait()
                      self.currentEve nt.eventWork()
                      self.currentEve nt.clear()
                      >
                      def eventOneWork():
                      # do some event 1 specific work here
                      >
                      def eventTwoWork():
                      # do some event 2 specific work here
                      >
                      if __name__ == "__main__":
                      events = [EventWrapper(ev entOneWork),Eve ntWrapper(event TwoWork)]
                      >
                      evtHandlerThrea d = eventhandler(ev ents)
                      evtHandlerThrea d.start()
                      >
                      # do something here #
                      evtHandlerThrea d.currentEvent. set()
                      # do more stuff here #
                      evtHandlerThrea d.currentEvent. set()
                      >
                      </code>
                      >
                      So you have a EventWrapper class that now contains the Event object
                      and a workEvent() method which is assigned to a function you create.
                      >
                      THanks a lot! Does this have to have event1 and event2 occur in
                      sequence? Will this still work even if only event2 occurs and event1
                      never occurs?
                      thanks
                      mark
                      well if event1 never occurs then it will block/wait until forever and
                      even if event2 has occurred you never know about it until event1
                      occurs. You can introduce a timeout to the wait() call on the event
                      object which says, "block X seconds or until event happens (someone
                      calls the set method)" so even if event1 doesnt occur you will execute
                      event1.eventWor k() because the timeout occurred. The way to fix this
                      is before you call the eventWork() method check if the event has
                      occurred via the isSet() method of Event objects.



                      Cheers

                      Comment

                      • Steve Holden

                        #12
                        Re: getting a thread out of sleep

                        placid wrote:
                        On Feb 22, 12:08 pm, mark <rkmr...@gmail. comwrote:
                        >On 21 Feb 2007 16:10:51 -0800, placid <Bul...@gmail.c omwrote:
                        >>
                        >>On Feb 22, 10:20 am, mark <rkmr...@gmail. comwrote:
                        >>>On 21 Feb 2007 14:47:50 -0800, placid <Bul...@gmail.c omwrote:
                        >>>>On Feb 22, 3:23 am, mark <rkmr...@gmail. comwrote:
                        >>>>>On 20 Feb 2007 21:26:18 -0800, placid <Bul...@gmail.c omwrote:
                        >>>>>>On Feb 21, 4:21 pm, "placid" <Bul...@gmail.c omwrote:
                        >>>>>>>On Feb 21, 4:12 pm, mark <rkmr...@gmail. comwrote:
                        >>>>>>>>On 20 Feb 2007 20:47:57 -0800, placid <Bul...@gmail.c omwrote:
                        >>>>>>>>>On Feb 21, 3:08 pm, mark <rkmr...@gmail. comwrote:
                        >>>>>>>>>>Rig ht now I have a thread that sleeps for sometime and check if an
                        [...]
                        Perhaps in future we can avoid quoting the whole preceding thread except
                        when strictly necessary?

                        regards
                        Steve
                        --
                        Steve Holden +44 150 684 7255 +1 800 494 3119
                        Holden Web LLC/Ltd http://www.holdenweb.com
                        Skype: holdenweb http://del.icio.us/steve.holden
                        Blog of Note: http://holdenweb.blogspot.com
                        See you at PyCon? http://us.pycon.org/TX2007

                        Comment

                        Working...