簡體   English   中英

Python隊列雙線程

[英]Python Queue double threading

我試圖運行5個線程,每個線程另外運行10個線程。 就像是:

dictionaryFileQueue = Queue.Queue()
with open(dictionaryFile, "r") as wordsToCheck:
    for word in wordsToCheck:
        dictionaryFileQueue.put(word)

for wordToScan in wordsList:
#...some code

    dictionaryFileOpen = dictionaryFileQueue

    for i in range(10):
        i_thread = Thread(target=words_Scan_Start, args=(dictionaryFileOpen, wordToScan))
        threads.append(i_thread)

for thread in threads:
    for wordThread in thread:
        wordThread.start()

def words_Scan_Start(dictionaryFile, wordToScan):
    # Here I need to make an action with "wordToScan".
    # But in 10 threads "range(10)" and use the same "dictionaryFileOpen" for every thread.
    #...............

但是,我需要每個wordToSacn線程都使用其自己的dictionaryFileQueue ,它來自公共源(第一行的dictionaryFileQueue )。 因為當我啟動所有線程時,它們都只使用一個隊列。 但是我需要每個wordToScan的Queue副本。 如何使每個wordToScan僅使用Queue的副本並從第一行開始枚舉? 如何使用來自共同來源的自己的Queue?

事實證明,這很容易做到。 只需粘貼以下部分:

dictionaryFileQueue = Queue.Queue()

for word in linesOfDictionary:
    dictionaryFileQueue.put(word.strip())

在這部分里面:

for wordToScan in wordsList:
#...some code

    # INSTEAD OF THIS LINE BELOW
    #dictionaryFileOpen = dictionaryFileQueue

並在腳本開頭添加以下代碼:

openDictionaryFile = open(dictionaryFile, "r")
linesOfDictionary = openDictionaryFile.readlines()
openDictionaryFile.close()

代替:

#dictionaryFileQueue = Queue.Queue()
#with open(dictionaryFile, "r") as wordsToCheck:
    #for word in wordsToCheck:
        #dictionaryFileQueue.put(word)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM