Thread allready started ??

Thomas Weholt 2002 at weholt.org
Tue Oct 1 20:37:36 CEST 2002


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.

Allready looking at recipies in the Python Cookbook, trying to hack one of
these into something like the code below, which is failing by the way.

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

import threading, time

class TaskManager(threading.Thread):
    def __init__(self, tasks):
        threading.Thread.__init__(self)
        self.tasks = tasks
        self.active = 1
        self.wait = 5

    def run(self):
        while self.active:
            for task in self.tasks:
                if task.doRun():
                    task.start()
            time.sleep(self.wait)

if __name__ == '__main__':

    class T(threading.Thread):

        def __init__(self):
            threading.Thread.__init__(self)
            self.active = 1

        def doRun(self):
            return 1

        def run(self):
            print "GO :%s" % self._Thread__name

    threads = []
    for i in range(0, 10):
        threads.append(T())

    tm = TaskManager(threads)
    tm.start()
    tm.join()

    print "Main thread  is ", tm.isAlive()





More information about the Python-list mailing list