Python:如何在一个线程出错后添加新线程

Python:如何在一个线程出错后添加新线程,python,multithreading,Python,Multithreading,我正在尝试创建线程循环,到目前为止,代码还不错。但由于某些异常,当线程退出时,我遇到了问题 现在,我正试图找出在一个线程因异常而退出后如何启动其他线程。我确实浏览了一下,但没有找到任何适用于这个复杂代码的示例。任何帮助都会很好 若线程已停止且队列不为空,则重新启动已停止的线程并继续执行列表的其余部分 这是我的代码: some_list = [1,2,3,4,5,6,7,8] exitFlag = 0 class threads(): @staticmethod def proce

我正在尝试创建线程循环,到目前为止,代码还不错。但由于某些异常,当线程退出时,我遇到了问题

现在,我正试图找出在一个线程因异常而退出后如何启动其他线程。我确实浏览了一下,但没有找到任何适用于这个复杂代码的示例。任何帮助都会很好

若线程已停止且队列不为空,则重新启动已停止的线程并继续执行列表的其余部分

这是我的代码:

some_list = [1,2,3,4,5,6,7,8]
exitFlag = 0
class threads():
    @staticmethod
    def process_data(threadName, q,queueLock):
        workQueue = q
        while not exitFlag:
            queueLock.acquire()
            if not workQueue.empty():
                data = q.get()
                queueLock.release()
                print "%s processing %s" % (threadName, data)
            else:
                queueLock.release()
            sleep(1)

    def run_threads(self):
        threadList = ["Thread-1", "Thread-2", "Thread-3"]
        nameList = some_list
        queueLock = threading.Lock()
        workQueue = Queue.Queue(1000000)
        threads = []
        threadID = 1

        # Create new threads
        for tName in threadList:
            thread = myThread(threadID, tName, workQueue,queueLock)
            thread.start()
            threads.append(thread)
            threadID += 1

        # Fill the queue
        queueLock.acquire()
        for word in nameList:
            workQueue.put(word)
        queueLock.release()

        # Wait for queue to empty
        while not workQueue.empty():
            pass

        # Notify threads it's time to exit
        global exitFlag
        exitFlag = 1

        # Wait for all threads to complete
        for t in threads:
            t.join()
        print "Exiting Main Thread"


class myThread (threading.Thread,threads):
    def __init__(self, threadID, name, q,queueLock):
        self.thread = threading.Thread(target=self.run)
        threading.Thread.__init__(self,target=self.run)
        self.threadID = threadID
        self.queueLock = queueLock
        self.name = name
        self.q = q

    def run(self):
       print "Starting " + self.name
       threads.process_data(self.name, self.q,self.queueLock)
       print "Exiting " + self.name

threads().run_threads()

像这样的方法应该会奏效:

...
    # Wait for queue to empty
    while not workQueue.empty():
        for (i, t) in enumerate(threads):
            if not t.is_alive():
                print("Recreating thread " + t.name)
                thread = myThread(threadID, threadList[i], workQueue,queueLock)
                thread.start()
                threads[i] = thread
                threadID += 1
...
我建议将线程起始代码放入某种方法中,因为它现在将被复制并且很难维护


这里的问题是,您可能会“丢失”致命线程从队列中弹出的数据。

您是否尝试在所有线程上检查
thread.is_alive()
,然后重新创建死掉的线程?您是否可以更清楚地指定您的期望值。你到底希望达到什么样的行为?举个例子会很有用。是的,我现在正试图找出在哪里添加
thread.isAlive()
,以及如何重新创建死掉的线程。。。如果线程停止且q未完成,请重新创建它。。像这样
Thread-3处理4开始Thread-1
Yes这就是解决方案。我将为线程启动代码创建方法。Tnx!