Ore*_*ail 4 python multithreading subclass kwargs
我正在尝试使用提供的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()
Run Code Online (Sandbox Code Playgroud)
但是,如果我运行如下:
st = StoppableThread(target=func)
Run Code Online (Sandbox Code Playgroud)
我明白了:
TypeError:
__init__()得到一个意外的关键字参数'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()
Run Code Online (Sandbox Code Playgroud)
这会将位置和关键字参数传递给基类.