Note that there are some explanatory texts on larger screens.

plurals
  1. POsimple thread management within python classes
    text
    copied!<p>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.</p> <p>what i want to know is two fold, <strong><em>how should i call this class elegantly and how should i stop these threads im creating?</em></strong></p> <p>this is what im currently doing:</p> <pre><code>tmp1 = textprint("hello this is text") tmp1.start() # do something tmp1.stop() </code></pre> <p>these are the options ive considered and <a href="http://docs.python.org/library/threading.html" rel="nofollow">looked</a> into so far:</p> <ul> <li><p>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.)</p> <p>sending a thread.event ? - from reading the docs i see an event can be sent, perhaps that can be used to stop it?</p> <p>or a <a href="http://www.python.org/dev/peps/pep-0343/" rel="nofollow">with</a> 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.</p></li> </ul> <p>what i would <em>like</em> to do is something like this:</p> <p><code>echo('hello')</code> (prints progress bar etc) - and then when i want to stop it <code>echo.stop()</code></p> <p>the obv. problem there though is that the stop function doesnt know which thread it is trying to stop.</p> <p>Here is a skeleton of what im trying to do:</p> <pre><code>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" </code></pre> <p>and then call it like so:</p> <pre><code>echo('this is text') </code></pre> <p>i also guess to do this i would have to</p> <pre><code>import echo from print_text </code></pre> <p>the WITH way of doing things suggests putting an <code>__enter__</code> and <code>__exit__</code> bit in. i tried them and they didnt work and also, i didnt know what i was doing, really appreciate any help, thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload