0
我試圖根據一定的條件重複執行特定操作的python線程。如果條件不滿足,則該線程應退出。我寫了下面的代碼,但它的運行無限期。從外部控制python線程的運行時間
class dummy(object):
def __init__(self):
# if the flag is set to False,the thread should exit
self.flag = True
def print_hello(self):
while self.flag:
print "Hello!! current Flag value: %s" % self.flag
time.sleep(0.5)
def execute(self):
t = threading.Thread(target=self.print_hello())
t.daemon = True # set daemon to True, to run thread in background
t.start()
if __name__ == "__main__":
obj = dummy()
obj.execute()
#Some other functions calls
#time.sleep(2)
print "Executed" # This line is never executed
obj.flag = False
我是python線程模塊的新手。我已經通過一些文章暗示使用threading.Timer()
函數,但這不是我所需要的。