栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python线程self._stop()'Event'对象不可调用

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python线程self._stop()'Event'对象不可调用

在上面提到的答案的注释中提到了该解决方案:

self._stop
已被所使用
Threading.thread

修改后的代码有效(带有注释的更改):

import sysimport threadingimport timeimport loggingclass StoppableThread(threading.Thread):    """Thread class with a stop() method. The thread itself has to check    regularly for the stopped() condition."""    def __init__(self):        print( "base init", file=sys.stderr )        super(StoppableThread, self).__init__()        self._stopper = threading.Event()          # ! must not use _stop    def stopit(self):        #  (avoid confusion)        print( "base stop()", file=sys.stderr )        self._stopper.set()  # ! must not use _stop    def stopped(self):        return self._stopper.is_set()   # ! must not use _stopclass datalogger(StoppableThread):    """    """    import time    def __init__(self, outfile):      """      """      StoppableThread.__init__(self)      self.outfile = outfile      print( "thread init", file=sys.stderr )    def run(self):      """      """      print( "thread running", file=sys.stderr )      while not self.stopped():        print( self.outfile , file=sys.stderr)        time.sleep(0.33)      print( "thread ending", file=sys.stderr )test = datalogger("test.txt")test.start()time.sleep(3)logging.debug("stopping thread")test.stopit()     #  (avoid confusion)logging.debug("waiting for thread to finish")test.join()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/660673.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号