python - 可停止线程错误

标签 python multithreading python-2.7

我正在创建一个可停止线程类:

import threading

class StoppableThread(threading.Thread):
    """Thread class with a stop() method. The thread itself has to check
    regularly for the stopped() condition."""

    def __init__(self):
        super(StoppableThread, self).__init__()
        self._stop = threading.Event()

    def stop(self):
        self._stop.set()

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

但是当我像这样创建对象时:

我收到此错误:

Traceback (most recent call last):
  File "./myFile.py", line 81, in <module>
    aObject = StoppableThread(target, args=("foo", "bar",)))
TypeError: __init__() got an unexpected keyword argument 'args'

提前谢谢您。

最佳答案

您已经重写了 init 方法,并且那里没有“args”参数。

你必须做这样的事情:

 def __init__(self, group=None, target=None, name=None, args=(), kwargs={}):
        super(StoppableThread, self).__init__(group, target, name, args, kwargs)
        self._stop = threading.Event()

关于python - 可停止线程错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34382655/

相关文章:

python-2.7 - Selenium Python 测试批处理文件未在任务计划程序中运行,浏览器未打开。批处理文件在没有任务计划程序的情况下运行正常

python - 如何显示指定目录中的文件列表

multithreading - 为什么我的事件监视器显示 Node.js 使用多线程?

c++ - Thread - 它是静态持续时间对象吗?

c# - HashAlgorithm.ComputeHash() 是线程安全的吗?

python插入函数列表理解

python - 屏蔽/隐藏 github 访问 token

python - 字典值缺失

python - Elasticsearch 的upsert函数?

python - Pandas 多索引: print all of first index if in second index