我正在创建一个可停止线程类:
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/