python中生产者消费者线程问题
高洛峰
高洛峰 2017-04-18 10:27:07
0
2
639

在使用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:
                #通知生产者结束
                #如何实现?
            
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
PHPzhong

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.

PHPzhong

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template