Terminating a thread from the parent

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

    #1

    Terminating a thread from the parent

    Hello,

    I have an app with embedded Python. Python scripts create their own
    threads and I need to terminate these threads at the point where the
    user wants to leave the application. I use threading.Threa d as base
    classes.

    I have tried to use call the join method of the python thread objects
    from C++. But although the call succeeds, the threads don't exit.

    What is the proper way of doing this ? (e.g. how does the python shell
    do this ? )

    Thanks in advance,

    Devrim.

  • Peter Hansen

    #2
    Re: Terminating a thread from the parent

    DE wrote:[color=blue]
    > I have an app with embedded Python. Python scripts create their own
    > threads and I need to terminate these threads at the point where the
    > user wants to leave the application. I use threading.Threa d as base
    > classes.
    >
    > I have tried to use call the join method of the python thread objects
    > from C++. But although the call succeeds, the threads don't exit.[/color]

    join() waits until the thread terminates, but it doesn't cause it to
    terminate.
    [color=blue]
    > What is the proper way of doing this ? (e.g. how does the python shell
    > do this ? )[/color]

    You have to poll for a termination request in the thread's main loop,
    and have the thread terminate itself (by returning from the target
    function or from the run() method, depending on which technique you used
    to create the Thread in the first place). Threads cannot be forcibly
    terminated**.

    There are numerous examples of this in the archives and probably a
    Cookbook recipe or two about it, if you look. Otherwise someone can
    post an example here.

    -Peter

    ** The exception is Threads on which .setDaemon(True ) has been called,
    which will terminate immediately when the main thread exits (i.e. when
    the entire process terminates), though that might not be helpful to you
    in your particular situation.

    Comment

    • flupke

      #3
      Re: Terminating a thread from the parent

      DE wrote:[color=blue]
      > Hello,
      >
      > I have an app with embedded Python. Python scripts create their own
      > threads and I need to terminate these threads at the point where the
      > user wants to leave the application. I use threading.Threa d as base
      > classes.
      >
      > I have tried to use call the join method of the python thread objects
      > from C++. But although the call succeeds, the threads don't exit.
      >
      > What is the proper way of doing this ? (e.g. how does the python shell
      > do this ? )
      >
      > Thanks in advance,
      >
      > Devrim.
      >[/color]

      I found this example somewhere. It shows how you terminate a thread.
      As Peter said, it's the thread that terminates itself.

      #!/usr/bin/env python
      """
      testthread.py
      An example of an idiom for controling threads

      Doug Fort

      """

      import threading

      class TestThread(thre ading.Thread):
      """
      A sample thread class
      """

      def __init__(self):
      """
      Constructor, setting initial variables
      """
      self._stopevent = threading.Event ()
      self._sleepperi od = 1.0

      threading.Threa d.__init__(self , name="TestThrea d")

      def run(self):
      """
      overload of threading.threa d.run()
      main control loop
      """
      print "%s starts" % (self.getName() ,)

      count = 0
      while not self._stopevent .isSet():
      count += 1
      print "loop %d" % (count,)
      self._stopevent .wait(self._sle epperiod)

      print "%s ends" % (self.getName() ,)

      def join(self,timeo ut=None):
      """
      Stop the thread
      """
      self._stopevent .set()
      threading.Threa d.join(self, timeout)

      if __name__ == "__main__":
      testthread = TestThread()
      testthread.star t()

      import time
      time.sleep(10.0 )

      testthread.join ()

      Benedict

      Comment

      • DE

        #4
        Re: Terminating a thread from the parent

        I appreciate your posts guys. It answers my questions and I like the
        idea of overriding join method. I will use this one.

        Comment

        Working...