python 继承线程的类 不能通过标志位结束线程
澳门银河官网
澳门银河官网
当前位置 : 澳门银河官网 > 澳门银河官网

python 继承线程的类 不能通过标志位结束线程

在测试生产消费者模型的时候遇到这样一个问题,在继承线程后 加了个标志位 mark

class Consumerthreading.Thread:
    def __init__self, queue:
        threading.Thread.__init__self
        self._queue = queue
        self.mark = True

    def runself:
        while self.mark:
            msg = self._queue.get
            if isinstancemsg, str and msg == quit:
                break
            print"Im a thread, and I received %s!!" % msg

        printBye byes!
def producer:
    q = queue.Queue
    worker = Consumerq
    worker.start  # 开启消费者线程
    start_time = time.time
    while time.time - start_time < 5:
        q.putsomething at %s % time.time
        time.sleep1
    worker.mark = Flese
    worker.join

我原先指望 通过这个标志位来实现线程的结束控制,但实际效果是程序卡死在worker.join
完全没有退出。

请教下,这是什么原因?

class Consumerthreading.Thread:
    def __init__self, queue:
        threading.Thread.__init__self
        self._queue = queue
        self.mark = True

    def runself:
        while self.mark:
            try:
                msg = self._queue.getblock=False # 非阻塞
                print"Im a thread, and I received %s!!" % msg
            except:pass
            
        printself.mark,self.mark
        printBye byes!
        
def producer:
    q = queue.Queue
    worker = Consumerq
    worker.start  # 开启消费者线程
    start_time = time.time
    while time.time - start_time < 5:
        q.putsomething at %s % time.time
        time.sleep1
    worker.mark = False
    worker.join

栏目列表

广告位

澳门银河官网