简体   繁体   English

python 3.5的绑定缓冲区(生产者/消费者)

[英]Bounded Buffer (producer/consumer) with python 3.5

I'm writing the classic producer/consumer problem in Python 3.5 using a deque as buff and 2 process that should work in parallel but unfortunately only the producer works, while the consumer doesn't "consume".. where am I wrong? 我在Python 3.5中编写经典的生产者/消费者问题,使用双端队列buff和2进程应并行工作,但不幸的是只有生产者可以工作,而消费者却没有“消费” ..我在哪里错? This is my code: 这是我的代码:

from time import sleep
import random
from collections import deque
import multiprocessing

MAX_LEN = 10
buff = deque(maxlen=MAX_LEN)

class producer:
    while True:
        if len(buff) == MAX_LEN:
            print("Producer: The buff is full, waiting...")
            sleep(10)
        buff.append(random.randint(1,9))


class consumer:
    while True:
        print("Consumer: hi")
        if len(buff) == 0:
            print("Consumer: The buff is empty, waiting...")
            sleep(10)
        buff.pop()



if __name__ == '__main__':
    multiprocessing.Process(target=producer).start().join()
    multiprocessing.Process(target=consumer).start().join()

and the result of this is: 其结果是:

Producer: The buff is full, waiting...
Producer: The buff is full, waiting...
Producer: The buff is full, waiting...
Producer: The buff is full, waiting...
Producer: The buff is full, waiting...
Producer: The buff is full, waiting...
^CTraceback (most recent call last):
  File "./boundedbuffer.py", line 9, in <module>
    class producer:
  File "./boundedbuffer.py", line 13, in producer
    sleep(10)
KeyboardInterrupt

Just for now I want to implement in this way, as an exercise, just to see if I understand the argument, although I know that it is not the most correct and that I should use semaphores or monitors. 到目前为止,我想以这种方式作为练习,以了解我是否理解该论点,尽管我知道这不是最正确的,并且应该使用信号量或监视器。 Maybe later I will try to implement it differently. 也许以后我会尝试以不同的方式实现它。 Thank you all in advance for your help and good evening :) 预先感谢大家的帮助和晚上好:)

You have defined classes with while True: loops in their bodies. 您已经使用while True:循环在它们的主体中定义了类。

The interpreter will execute the first while loop in your file forever. 解释器将永久执行文件中的第一个while循环。

multiprocessing.Process expects a callable as its target argument, so if you change the producer and consumer classes to functions the if __ name __ == __ main __ : block will be executed (though you still may not get the outcome that you expect). multiprocessing.Process期望将callable作为其目标参数,因此,如果将生产者类和使用者类更改为函数, if __ name __ == __ main __ :块(尽管您可能仍未获得预期的结果) 。

def producer():
    while True:
        if len(buff) == MAX_LEN:
            print("Producer: The buff is full, waiting...")
            sleep(10)
        buff.append(random.randint(1,9))


def consumer():
    while True:
        print("Consumer: hi")
        if len(buff) == 0:
            print("Consumer: The buff is empty, waiting...")
            sleep(10)
        buff.pop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM