我试图使用StoppableThread类表示为an answer to another question
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__()得到了一个意想不到的关键字参数“目标”
可能是对该如何使用的疏忽。
发布于 2013-03-17 15:27:55
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()这将将位置参数和关键字参数传递给基类。
发布于 2013-03-17 15:29:34
您正在重写init,而您的init不带任何参数。您应该添加一个“目标”参数,并通过*args和*kwargs将其传递给您的基类构造函数,该构造函数具有超级或更好的允许任意参数。
也就是说。
def __init__(self,*args,**kwargs):
super(threading.Thread,self).__init__(*args,**kwargs)
self._stop = threading.Event()https://stackoverflow.com/questions/15462486
复制相似问题