0

私は生産者/消費者アプリケーションを書いています。プロデューサースレッドは正常に起動しますが、コンシューマースレッドを起動しようとすると、例外が発生します。関連するコードは次のとおりです。

#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

#残りの処理コード

正しいインデントを取得できませんでした。コードでは問題ありません。

4

1 に答える 1

0

これはばかげた間違いでした (おそらく、私は PHP で多くのコーディングを行ってきたためです)。次のように配列を初期化しました。

self.producerThreads = self.consumerThreads = []

両方の配列が同じメモリを参照していました。

于 2012-10-30T10:19:43.600 に答える