Multithreading python中的生产者-消费者

Multithreading python中的生产者-消费者,multithreading,python-3.x,producer-consumer,Multithreading,Python 3.x,Producer Consumer,对于生产者-消费者问题,我提出了以下解决方案: import threading import random import time class Bucket: def __init__(self, size): self.size = size self.current_size = 0 self.cond_var = threading.Condition() def available_for_put(self): return self.curren

对于生产者-消费者问题,我提出了以下解决方案:

import threading
import random
import time

class Bucket:

def __init__(self, size):
    self.size = size
    self.current_size = 0
    self.cond_var = threading.Condition()

def available_for_put(self):
    return self.current_size < self.size

def available_for_get(self):
    return self.current_size > 0

def put(self):
    self.current_size = self.current_size + 1
    print(self)
    self.cond_var.notify_all()

def get(self):
    self.current_size = self.current_size - 1
    print(self)
    self.cond_var.notify_all()

def acquire(self):
    self.cond_var.acquire()

def release(self):
    self.cond_var.release()

def wait(self):
    self.cond_var.wait()

def __str__(self):
    return "Size is {0}".format(self.current_size)

class Worker(threading.Thread):


PRODUCER = 1
CONSUMER = 0

def __init__(self, bucket, kind):
    threading.Thread.__init__(self)
    self.kind = kind
    self.bucket = bucket

def run(self):
    while(1):
        self.bucket.acquire()
        while(((self.kind == Worker.PRODUCER) and (not self.bucket.available_for_put())) or \
            ((self.kind == Worker.CONSUMER) and (not self.bucket.available_for_get()))):
            self.bucket.wait()
            ### self.bucket.acquire()
        if self.kind == Worker.PRODUCER:
            self.bucket.put()
        else:
            self.bucket.get()
        time.sleep(0.1)
        self.bucket.release()


bucket = Bucket(10)
workers = []

for i in range(10):
    workers.append(Worker(bucket, i % 2))

for w in workers:
    w.start()
    print("Thread started")

for w in workers:
    w.join()
导入线程
随机输入
导入时间
类桶:
定义初始值(自身,大小):
self.size=大小
self.current_size=0
self.cond_var=threading.Condition()
def可用于def put(自身):
返回self.current_size0
def put(自):
self.current\u size=self.current\u size+1
打印(自我)
self.cond_var.notify_all()
def get(自我):
self.current\u size=self.current\u size-1
打印(自我)
self.cond_var.notify_all()
def获取(自我):
self.cond_var.acquire()
def释放(自):
self.cond_var.release()
def等待(自我):
self.cond_var.wait()
定义(自我):
返回“大小为{0}”。格式(self.current_Size)
类工作线程(threading.Thread):
生产者=1
消费者=0
定义初始(自身、桶、种类):
threading.Thread.\uuuuu init\uuuuuu(自)
善良
self.bucket=bucket
def运行(自):
而(一):
self.bucket.acquire()
而(((self.kind==Worker.PRODUCER)和(非self.bucket.available\u for_put())或\
((self.kind==Worker.CONSUMER)和(非self.bucket.available\u for_get()):
self.bucket.wait()
###self.bucket.acquire()
如果self.kind==Worker.PRODUCER:
self.bucket.put()
其他:
self.bucket.get()
睡眠时间(0.1)
self.bucket.release()
铲斗=铲斗(10)
工人=[]
对于范围(10)内的i:
附加工人(工人(桶,i%2))
对于在职工人:
w、 开始()
打印(“线程启动”)
对于在职工人:
w、 加入

显然,如果我删除
while(1)
循环,让每个线程在循环中运行块,只有当它达到死锁时,我不明白为什么。

显然,问题是在从等待中醒来后,你重新获得锁,因此现在注释掉的acquire将被阻塞。

生产者-消费者模式可以使用Python的内置工具轻松实现队列支持:

这可以简化您的代码。调度器也非常有用:

由于您的问题是用Python-3.x标记的,因此您肯定应该看看concurrent.futures模块:

你的工人可以是任务,而桶可以变成队列