未启动线程,但Python抛出异常'线程已经启动'

2024-06-26 13:30:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在写一个生产者消费者应用程序。生产者线程开始的很好,但是当我尝试启动消费者线程时,我得到了一个异常。以下是相关代码:

#Producer threads
    for i in range(nThreads):
        self.producerThreads.append(MailThread(i, settings.MAX_EMAILS_PERPASS, self.toFetchQueue, self.rawEmailsQueue, self.stopEvent))
        self.producerThreads[i].start()
        logging.info('Started producer thread %d', i)

    #Consumer threads
    #for i in range(settings.MAX_CONS_THREADS):
    try:
        self.consumerThreads.append(ProcessThread(i, settings.STORE_DIRECTORY, settings.DELETE_ONPIPE, self.rawEmailsQueue, self.stopEvent))
        self.consumerThreads[i].start()
        logging.info('Started consumer thread %d', i)
    except Exception, e:
        logging.error('Failed to start consumer thread %s', str(e))

以下是消费者类别:

import logging, commands, threading, uuid, os, settings, Queue

class ProcessThread(threading.Thread):
"""
Class to process the emails. 
"""
def __init__(self, threadCount, storeDirectory, deleteOnPipe, rawEmailsQueue, stopEvent):
    self.threadCount = threadCount
    self.rawEmailsQueue = rawEmailsQueue
    self.stopEvent = stopEvent
    self.storeDirectory = storeDirectory
    self.deleteOnPipe = deleteOnPipe
    threading.Thread.__init__(self)

def run(self):
    logging.info('Run process for consumer thread %d', self.threadCount)

    while not self.stopEvent.is_set():
        try:
            emailContainer = rawEmailsQueue.get(False)
            logging.debug('Got a new email')

        except Queue.Empty:
            logging.debug('No emails in queue, going to sleep for a while')
            sleep(0.1)
            continue

#其余处理代码

我不能得到正确的缩进,在我的代码中是可以的


Tags: to代码inselfinfoforsettingsconsumer