我正在尝试使用显示为 an answer to another question 的 StoppableThread
类:
import threading
# Technique for creating a thread that can be stopped safely
# Posted by Bluebird75 on StackOverflow
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()
但是,如果我运行类似的东西:
st = StoppableThread(target=func)
我得到:
TypeError:
__init__()
got an unexpected keyword argument 'target'
可能是对如何使用它的疏忽。
最佳答案
StoppableThread
类不会在构造函数中采用或传递任何附加参数给 threading.Thread
。你需要做这样的事情:
class StoppableThread(threading.Thread):
"""Thread class with a stop() method. The thread itself has to check
regularly for the stopped() condition."""
def __init__(self,*args,**kwargs):
super(threading.Thread,self).__init__(*args,**kwargs)
self._stop = threading.Event()
这会将位置参数和关键字参数传递给基类。
关于python - 父类(super class) __init__ 不认识它的 kwargs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15462486/