Terminating a thread from the parent

Discussion in 'Python' started by DE, May 23, 2005.

  1. DE

    DE Guest

    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.Thread 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.
    DE, May 23, 2005
    #1
    1. Advertising

  2. DE

    Peter Hansen Guest

    DE wrote:
    > 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.Thread 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.


    join() waits until the thread terminates, but it doesn't cause it to
    terminate.

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


    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.
    Peter Hansen, May 23, 2005
    #2
    1. Advertising

  3. DE

    flupke Guest

    DE wrote:
    > 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.Thread 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.
    >


    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
    http://www.dougfort.net
    """

    import threading

    class TestThread(threading.Thread):
    """
    A sample thread class
    """

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

    threading.Thread.__init__(self, name="TestThread")

    def run(self):
    """
    overload of threading.thread.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._sleepperiod)

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

    def join(self,timeout=None):
    """
    Stop the thread
    """
    self._stopevent.set()
    threading.Thread.join(self, timeout)

    if __name__ == "__main__":
    testthread = TestThread()
    testthread.start()

    import time
    time.sleep(10.0)

    testthread.join()

    Benedict
    flupke, May 24, 2005
    #3
  4. DE

    DE Guest

    I appreciate your posts guys. It answers my questions and I like the
    idea of overriding join method. I will use this one.
    DE, May 24, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman

    Terminating a thread from the main thread

    Charles A. Lackman, Dec 9, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    1,527
    Herfried K. Wagner [MVP]
    Dec 9, 2004
  2. Uppu
    Replies:
    0
    Views:
    344
  3. Manisha
    Replies:
    2
    Views:
    512
  4. Jeffrey Barish
    Replies:
    0
    Views:
    293
    Jeffrey Barish
    May 28, 2004
  5. Ian Wilson
    Replies:
    4
    Views:
    626
    Ian Wilson
    Jan 24, 2007
Loading...

Share This Page