search

Home  >  Q&A  >  body text

python中生产者消费者线程问题

在使用python的多线程时,使用了生产者消费者模式,一般都是消费者接受生产者的数据执行某些操作,但是现在这个消费者线程遇到了异常,需要终止执行,但是生产者线程因为还在生产数据,主线程在等待它执行完。目前想当消费者线程遇到错误时能够通知生产者线程,我挂了,你也结束吧。请问大家有什么好的实现方法

import threading

class Producer(threading.Thread):

    def __init__(self, queue):
        super(Producer, self).__init__()
        self.queue = queue

    def run(self):
        while True:
            for i in range(10):
                self.queue.put(i)

class Consumer(threading.Thread):

    def __init__(self, queue):
        super(Consumer, self).__init__()
        self.queue = queue

    def run(self):
        while True:
            try:
                data = self.queue.get()
                print data
                if data == 5:
                    raise ValueError('over')
            except ValueError as e:
                #通知生产者结束
                #如何实现?
            
高洛峰高洛峰2785 days ago637

reply all(2)I'll reply

  • PHPz

    PHPz2017-04-18 10:29:07

    My method:
    Add a flag logo.

    Let’s take a look at the results first:

    No more nonsense, show u the code

    #!/usr/bin/python
    # coding=utf-8
    import threading
    import time
    
    
    class Producer(threading.Thread):
    
        def __init__(self, queue, flag):
            super(Producer, self).__init__()
            self.queue = queue
            self.flag = flag
    
        def run(self):
            while True:
                length = max(self.queue) + 1
                print "============================= producer queue", self.queue
                self.queue.append(length)
                print 'flag length=', len(self.flag)
                if len(self.flag) == 0:
                    print "producer 我也结束了"
                    break
                time.sleep(2)
    
    
    class Consumer(threading.Thread):
    
        def __init__(self, queue, flag):
            super(Consumer, self).__init__()
            self.queue = queue
            self.flag = flag
    
        def run(self):
            while True:
                try:
                    length = len(self.queue)
                    print "consumer queue", self.queue
                    if length > 5:
                        self.flag.pop()  # 注意我是flag
                        raise ValueError('over')
                    self.queue.pop(0)
    
                except ValueError as e:
                    # 通知生产者结束
                    # 如何实现?
                    print "consumer 我结束了", e
                    break
                    # raise(e)
                time.sleep(4)
    
    queue = [1, 2, 3]
    
    flag = [0]  # 表示正常
    
    Consumer(queue, flag).start()
    time.sleep(1)
    Producer(queue, flag).start()
    

    Finally, let’s talk about python’s multi-threading. Due to the existence of GIL, multi-threading is actually sometimes not the best choice. There are a lot of information on the Internet about when to use it. The author can also choose more based on his own business situation. Thread module.

    reply
    0
  • PHPz

    PHPz2017-04-18 10:29:07

    To put it simply, just raise the error and let the process crash on its own.
    Or, you can also use exception handling to wrap the consumer's run, catch the exception, and then control the producer's thread. Alright.

    reply
    0
  • Cancelreply