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

在使用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教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回覆(2)
PHPzhong

我的方法:
加上一個 flag 標識。

先看結果吧:

更多廢話也不多說了,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()

最後說說python的多線程,由於GIL的存在,其實多線程有的時候並不是最好的選擇,具體什麼時候使用,網上也說的很多了,樓主也可以結合自己的業務情況捨取多線程模組。

PHPzhong

簡單的話,就直接把錯誤raise出來,然後讓進程自己崩潰掉就好了.
或者,你也可以用異常處理把消費者的run包裹起來,捕獲這個異常,然後再控制生產者的線程就好了.

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!