I have extended threading.Thread - my idea is to do something like this:
class StateManager(threading.Thread):
def run(self, lock, state):
while True:
lock.acquire()
self.updateState(state)
lock.release()
time.sleep(60)
I need to be able to pass reference to my "state" object and eventually to a lock (I'm quite new to multi-threading and still confused about the necessity of locking in Python). What is the proper way to do it?
解决方案
pass them in the constructor, e.g.
class StateManager(threading.Thread):
def __init__(self, lock, state):
threading.Thread.__init__(self)
self.lock = lock
self.state = state
def run(self):
lock = self.lock
state = self.state
while True:
lock.acquire()
self.updateState(state)
lock.release()
time.sleep(60)