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

标签 python multithreading python-3.4

尝试来自 https://stackoverflow.com/a/325528/1619432 的“可停止”线程像这样:

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

谁能解释一下我做错了什么?

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

最佳答案

解决方案在上面提到的答案的评论中提到:

self._stop 已被 Threading.thread 使用。

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

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' 对象不可调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30393612/

相关文章:

python - 查找 1 个且只有 1 个字符,Python

python - Numpy 索引 : Set values of an array given by conditions in different array

c++ - 我怎么知道哪个线程调用了方法

python - 我可以在 tkinter 中创建一个检查按钮来检查所有其他检查按钮吗?

python-2.7 - pip、easy_install 命令在 Ubuntu 中不起作用。安装了 Python 2.7 和 3.4

python - Cython: Segmentation Fault Using API Embedding Cython to C 语言

python - 如何在 tensorflow 中保存文本分类模型?

Java 多线程 - 从列表中删除项目

multithreading - TForm.Handle 线程安全吗?

python - 在没有外部模块的网站上查找信息