我扩展了 threading.Thread - 我的想法是做这样的事情:
class StateManager(threading.Thread):
def run(self, lock, state):
while True:
lock.acquire()
self.updateState(state)
lock.release()
time.sleep(60)
我需要能够传递对我的“状态”对象的引用,并最终传递给锁(我对多线程很陌生,并且仍然对Python中锁定的必要性感到困惑)。正确的做法是什么?
将它们传递到构造函数中,例如
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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)