Leider steht in der Api:
"The interval the timer will wait before executing its action may not be exactly the same as the interval specified by the user."
Welche ist denn die genauste Möglichkeit Zeitintervalle abzugeben?
Code: Alles auswählen
#!/usr/bin/env python
#-*- coding: iso-8859-1 -*-
import threading
import time
class Loop(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
print 'Start Init'
self.waitTime = 1
self.t = threading.Timer(self.waitTime, self.loopIt)
def start(self):
print 'Start Code'
self.t.start()
def startItAgain(self):
print 'Start startItAgain'
self.t = threading.Timer(self.waitTime, self.loopIt)
self.t.start()
def loopIt(self):
print 'loopIt'
self.t = threading.Timer(self.waitTime, self.loopIt)
self.t.start()
def stopIt(self):
print 'stopIt'
self.t.cancel()
class Application:
def __init__(self):
print "Main- Create and start counter"
loop = Loop()
loop.start()
time.sleep(5)
print "Main - Stop counter"
loop.stopIt()
print "Main - Still running 1"
print "Main - Still running 2"
time.sleep(5)
print "Main - Start counter again"
loop.startItAgain()
time.sleep(5)
print "Stop counter"
loop.stopIt()
print "Main - Program finished."
if __name__ == "__main__":
app = Application()
Code: Alles auswählen
#!/usr/bin/env python
#-*- coding: iso-8859-1 -*-
import threading
import time
class Loop(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.count = 0
# Values for self.running:
# 0 : Ready.
# 1 : Running.
# 2 : Finished.
self.running = 0
def run(self):
self.running = 1
self.loop()
def loop(self):
pass
print 'loop'
time.sleep(0.1)
if not self.running == 2:
self.loop()
class Application:
def __init__(self):
print "Create and start counter"
loop = Loop()
loop.start()
print "Main-Programm don«t need to wait"
time.sleep(1)
print "Main-Programm is still running - great"
time.sleep(1)
print "Stop counter"
loop.running = 2
time.sleep(1)
print "Start counter again"
# del loop
loop = Loop()
loop.start()
time.sleep(2)
print "Main-Programm should«t wait"
loop.running = 2
print "Main-Program finished."
Vielen Dank euch Alle!