簡體   English   中英

多線程與 Python

[英]MultiThreading with Python

這是一個生產者消費者問題。 我需要一個生產者和多個消費者來訪問共享數據單元,並且每個消費者都需要在生產者制作額外數據之前訪問生產的數據。 當只有一個消費者時,代碼可以正常工作。 我試圖列出生產者和消費者的列表,以便對它們進行 to.join() 和 .start()。 該程序對第一個消費者有效,但在到達第二個消費者時掛斷。 我試圖將getData和setData中的鎖定機制從“notify”更改為“notifyAll”,我是python的初學者,這些東西對我來說很陌生,但我已經嘗試了10個小時,真的很感激幫助。

import time, random
from threading import Thread, currentThread, Condition

class SharedCell(object):
    
    def __init__(self):
        self.data = -1
        self.writeable = True
        self.condition = Condition()
        
    def setData(self, data):
        self.condition.acquire()
        while not self.writeable:
            self.condition.wait()
        
        print("%s setting data to %d" % \
              (currentThread().getName(), data))
        self.data = data
        self.writeable = False
        self.condition.notifyAll()
        self.condition.release()
    
    def getData(self):
        self.condition.acquire()
        while self.writeable:
            self.condition.wait()
        print(f'accessing data {currentThread().getName()} {self.data}')
        self.writeable = True
        self.condition.notifyAll()
        self.condition.release()
        return self.data
    
class Producer(Thread):
    
    def __init__(self, cell, accessCount, sleepMax):
        Thread.__init__(self, name = "Producer")
        self.accessCount = accessCount
        self.cell = cell
        self.sleepMax = sleepMax
    
    def run(self):
        
        print("%s starting up" % self.getName())
        for count in range(self.accessCount):
            time.sleep(random.randint(1, self.sleepMax))
            self.cell.setData(count + 1)
        print("%s is done producing\n" % self.getName())
        
class Consumer(Thread):
    
    def __init__(self, cell, accessCount, sleepMax):
        Thread.__init__(self)
        self.accessCount = accessCount
        self.cell = cell
        self.sleepMax = sleepMax
        
    def run(self):

        print("%s starting up" % self.getName())
        for count in range(self.accessCount):
            time.sleep(random.randint(1, self.sleepMax))
            value = self.cell.getData()
        print("%s is done consuming\n" % self.getName())
        
def main():
    accessCount = int(input("Enter the number of accesses: "))
    sleepMax = 4
    cell = SharedCell()
    
    producer = Producer(cell, accessCount, sleepMax)
    consumer = Consumer(cell, accessCount, sleepMax)
    consumerTwo = Consumer(cell, accessCount, sleepMax)
    
    threads = []
    threads.append(producer)
    threads.append(consumer)
    threads.append(consumerTwo)
    
        
    print("Starting the threads")      
   
    for thread in threads:
        thread.start()
        thread.join()

main()

join function 阻塞當前線程並等待直到指示的線程終止。 在您的main function 結束時的循環中,為什么在啟動后立即join每個線程? 這將導致啟動線程 1,然后在啟動線程 2 之前等待它終止,然后在啟動線程 3 之前等待它終止,依此類推。

也許你的意思是這樣的:

for thread in threads:
    thread.start()

for thread in threads:
    thread.join()

以便在您等待它們終止之前啟動每個線程。

暫無
暫無

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

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