simple thread management within python classes
Question: Im trying to write a module for Python that prints out text for my program and
displays a progress bar while i do something in the background. Im using the
'threading' module currently but open to suggestions if something else will
make it easier.

what i want to know is two fold, **_how should i call this class elegantly and
how should i stop these threads im creating?_**

this is what im currently doing:

    
    
    tmp1 = textprint("hello this is text")
    tmp1.start()
    # do something
    tmp1.stop()
    

these are the options ive considered and
[looked](http://docs.python.org/library/threading.html) into so far:

  * using thread.name to find the name of the thread or having the thread return a name to kill afterwards. OR passing a number for similar thread identification afterwards. (a bit cumbersome and not my favourite solution.)

sending a thread.event ? - from reading the docs i see an event can be sent,
perhaps that can be used to stop it?

or a [with](http://www.python.org/dev/peps/pep-0343/) statement but im unclear
how to use it in this context, plus i find most of the python docs extremely
confusing and not written for me at all.

what i would _like_ to do is something like this:

`echo('hello')` (prints progress bar etc) \- and then when i want to stop it
`echo.stop()`

the obv. problem there though is that the stop function doesnt know which
thread it is trying to stop.

Here is a skeleton of what im trying to do:

    
    
    import time
    import string
    import threading
    
    class print_text(threading.Thread):
    
        def __init__(self,arg=None):
            super(print_text,self).__init__()
            self._stop = False
            self.arg=arg
    
        def run (self):
            # start thread for text
            print self.txt
            while not self._stop:
                    print "rude words"
    
        def echo (self,txt):
            self.txt=txt
            self.start()
    
        def stop(self):
            self._stop = True
    
        def stopped(self):
            return self._stop == True
    
        def __enter__(self):
            print "woo"
            return thing
    
        def __exit__(self, type, value, traceback):
            return isinstance(value, TypeError)
    
    
    if __name__ == '__main__':
    
        print_text.start.echo('this is text') # dunt werk
    
        with print_text.echo('this is text'):
               time.sleep(3)
    
        print "done"
    

and then call it like so:

    
    
    echo('this is text') 
    

i also guess to do this i would have to

    
    
    import echo from print_text 
    

the WITH way of doing things suggests putting an `__enter__` and `__exit__`
bit in. i tried them and they didnt work and also, i didnt know what i was
doing, really appreciate any help, thanks.


Answer: The best way to stop a thread in Python is to politely ask it to stop. The
best way to pass new data to a thread is with the `Queue` module.

Both are used in [the code in this
post](http://eli.thegreenplace.net/2011/05/18/code-sample-socket-client-
thread-in-python/), which demonstrates socket communication from a Python
thread but is otherwise relevant to your question. If you read the code
carefully you'll notice:

  1. Using `threading.Event()` which is set by a method call from outside, and which the thread periodically checks to know if it was asked to die.
  2. Using `Queue.Queue()` for both passing commands to the thread and receiving responses from it.

