Python线程self._stop()'Event'对象不可调用 [英] Python threading self._stop() 'Event' object is not callable

查看:155
本文介绍了Python线程self._stop()'Event'对象不可调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://stackoverflow.com/a/325528/1619432 中尝试可停止"线程,如下所示:

Trying a "stoppable" thread from https://stackoverflow.com/a/325528/1619432 like so:

import sys
import threading
import time
import logging

class 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._stop = threading.Event()

    def stop(self):
        print( "base stop()", file=sys.stderr )
        self._stop.set()

    def stopped(self):
        return self._stop.is_set()


class 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.stop()
logging.debug("waiting for thread to finish")
test.join()

给出以下输出错误:

> demo.py
base init
thread init
thread running
test.txt
test.txt
test.txt
test.txt
test.txt
test.txt
test.txt
test.txt
test.txt
test.txt
base stop()
thread ending
Traceback (most recent call last):
  File "demo.py", line 54, in <module>
    test.join()
  File "C:\Python34\lib\threading.py", line 1061, in join
    self._wait_for_tstate_lock()
  File "C:\Python34\lib\threading.py", line 1079, in _wait_for_tstate_lock
    self._stop()
TypeError: 'Event' object is not callable

有人可以解释我在做什么错吗?

Could someone please explain what I am doing wrong?

文档: https://docs.python.org/3.4 /library/threading.html#event-objects

推荐答案

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

The solution is mentioned in a comment to the answer referred to above:

self._stop已被Threading.thread使用.

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

This modified code works (changes marked with comments):

import sys
import threading
import time
import logging

class 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 _stop


class 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()

这篇关于Python线程self._stop()'Event'对象不可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆