(Synchronous) Thread Control

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

    (Synchronous) Thread Control

    Hi,

    I like to use thread to simplify the handling of independant,
    blocking tasks. But controling them from a main thread is
    not always easy to do in a clean way. So I've written some
    generic code whose purpose is to start and stop threads in
    a synchronous (blocking) way from the caller's point of view.

    Hence, after start() is called you are garanteed that the
    thread is running, and after stop() you know it has completed
    its tasks.

    At the end of this message, you'll find an example of the kind
    of code I'm using. Here is its output :

    Thread: starting
    Main: waiting 5 seconds
    Thread: beating
    Thread: beating
    Thread: beating
    Thread: beating
    Main: done sleeping, stopping thread
    Wrapper: joining thread
    Thread: beating
    Thread: exiting


    It seems like the main thread never exits from join(), in spite of
    the timeout and the likeliness of the sub thread ending.

    Any idea of what I've done wrong?

    Best regards,
    Olivier.



    import threading, time

    class IntThread:

    def start(self):
    launched = threading.Event () # Initially false
    self.listener = self.Listener(l aunched)
    self.listener.s tart()
    launched.wait()

    def stop(self):
    self.listener.s top()
    print "Wrapper: joining thread"
    self.listener.j oin(5) # Should be more than enought
    print "Wrapper: after join()"

    class Listener(thread ing.Thread):

    def __init__(self, event):
    threading.Threa d.__init__(self )
    self.do_stop = False
    self.event = event

    def stop(self):
    self.do_stop = True

    def run(self):
    print "Thread: starting"
    self.event.set( )
    while not self.do_stop:
    time.sleep(1)
    print "Thread: beating"
    print "Thread: exiting"

    def test():
    thd = IntThread()
    thd.start()
    delay = 5
    print "Main: waiting %s seconds" % delay
    time.sleep(dela y)
    print "Main: done sleeping, stopping thread"
    thd.stop()
    print "Main: end of program"

    test()
  • phansen

    #2
    Re: (Synchronous) Thread Control

    Olivier Parisy wrote:[color=blue]
    > It seems like the main thread never exits from join(), in spite of
    > the timeout and the likeliness of the sub thread ending.
    >
    > Any idea of what I've done wrong?[/color]

    Nothing at all. It works fine here. (And nice clean code
    you have, by the way.)

    Are you running this from the command line, or inside some
    other tool, or something else unusual? What versions of
    Python and OS are you using?

    I'm on Win XP Pro SP 2 with Python 2.3.4.

    -Peter

    Comment

    • Olivier Parisy

      #3
      Re: (Synchronous) Thread Control [Solved!]

      phansen wrote:[color=blue]
      > Olivier Parisy wrote:
      >[color=green]
      >> It seems like the main thread never exits from join(), in spite of
      >> the timeout and the likeliness of the sub thread ending.
      >>
      >> Any idea of what I've done wrong?[/color]
      >
      > Nothing at all. It works fine here. (And nice clean code
      > you have, by the way.)[/color]

      Hey, thanks! High-level languages are nice in that you can spend more
      time on polish, indeed :-)

      [color=blue]
      > Are you running this from the command line, or inside some
      > other tool, or something else unusual? What versions of
      > Python and OS are you using?[/color]

      Gosh, you are right: this definitely works fine in the command line.
      I was running this from a Linux file manager (using "launch in a
      terminal" and "keep the terminal open" options), but something
      seems to be wrong with this approach.

      Regards,
      Olivier.

      Comment

      Working...