繁体   English   中英

在正在运行的线程中设置变量以停止线程

[英]Setting a Variable inside a Running Thread to Stop the Thread

我有一个对象cookerrun()方法启动一个新的线程cookingThread 5秒钟后,我怎么能阻止cookingThread通过设置变量stopThread

尝试使用cooker.toggle()首先启动线程,但是下一个cooker.toggle()无法停止线程。

下面的代码给我错误NameError: global name 'cookingThread' is not defined

import threading
import time

class Cooker(object):

    def __init__(self, recipe):
        self.active = False
        self.recipe = recipe

    def toggle(self):
        if not self.active:
            self.active = True
            self.run()

        else:
            self.active = False
            # How can this stop flag be passed into the running thread?
            cookingThread.stopThread = True


    def run(self):
        cookingThread = CookingThread(self.recipe)
        cookingThread.start()

CookingThread

class CookingThread(threading.Thread):

    def __init__(self, recipe):
        super(CookingThread, self).__init__()
        self.recipe = recipe
        self.stopThread = False

    def run(self):
        for i in range(self.recipe):
            # Check if we should stop the thread
            if self.stopThread:
                return
            else:
                print 'Cooking...'
                time.sleep(1)

主要

cooker = Cooker(10)
cooker.toggle()    # Starts the thread

time.sleep(5)
cooker.toggle()    # Stops the thread (does not work)

问题是cookingThread是仅在Cooker.run()方法内部作用域的方法。 该错误表明您需要将其声明为全局方法,以便能够从所有方法进行访问。 但这不是一个好习惯。

您可以执行以下操作

import threading
import time

class Cooker(object):

    def __init__(self, recipe):
        self.active = False
        self.recipe = recipe

    def toggle(self):
        if not self.active:
            self.active = True
            self.run()

        else:
            self.active = False
            # How can this stop flag be passed into the running thread?
            self.cookingThread.stop()


    def run(self):
        self.cookingThread = CookingThread(self.recipe)
        self.cookingThread.start()

并如下更改CookingThread。

class CookingThread(threading.Thread):

    def __init__(self, recipe):
        super(CookingThread, self).__init__()
        self.recipe = recipe
        self.stopThread = False

    def run(self):
        for i in range(self.recipe):
            # Check if we should stop the thread
            if self.stopThread:
                return
            else:
                print 'Cooking...'
                time.sleep(1)

    def stop(self):
        self.stopThread = True

根据经验,开发面向对象程序设计时切勿直接访问字段(例如cookingThread.stopThread 尝试使用诸如stop的方法进行实际修改。

暂无
暂无

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

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