Thread allready started ??

Chris Liechti cliechti at gmx.net
Tue Oct 1 14:57:47 EDT 2002


"Thomas Weholt" <2002 at weholt.org> wrote in
news:QXlm9.28399$sR2.488144 at news4.ulv.nextra.no: 

> I'm trying to create a TaskManager, starting a bunch of threads on
> given intervals. The problem is that I get an error "Thread allready
> started". This TaskManager is running inside a threaded webserver, so
> I don't know if this is the best way to have a set of tasks repeated
> over time on given intervals. If anybody has any pointers I'd
> appreciate it. 

a Thread can only be started once. if you need it again, you need to create 
a new instance.

> NB! I want a Manager-thread starting/stopping sub-threads, not just a
> simple while loop.

you'll probably have a while loop ;-)

one way:
make supend/resume methods (untested):

class T(threading.Thread)
    	def __init__(self):
    	    	self.suspended = threading.Event()
    	    	self.active = 1
    	    	self.suspended.set()
    	def suspend(self):
    	    	self.running.clear()
    	def resume(self):
    	    	self.running.set()
    	def stop(self):
    	    	self.active = 0
    	    	self.resume()
    	def run(self):
    	    	while self.active:
    	    	    	work(with, you, data)
    	    	    	self.running.wait()    	#will return if event is set

there are many other ways...

chris

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list