1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| import threading
import time
class Timeout(threading.Thread):
def __init__(self, rate = 60):
threading.Thread.__init__(self)
self.rate = rate
self.stopped=False
def run(self):
while self.rate>0:
print " ",self.rate, time.time(), threading.enumerate()
time.sleep(self.rate)
self.stopped=True
print "timeout ", self.__str__(), "end"
def setrate(self, rate):
self.rate = rate
def isstopped(self):
return self.stopped
class Changerate(threading.Thread):
def __init__(self, timeoutthread):
threading.Thread.__init__(self)
self.timeouthread=timeoutthread
def run(self):
time.sleep(10)
self.timeouthread.setrate(1)
print "1", self.timeouthread.isAlive(),self.timeouthread.isstopped()
if self.timeouthread.isstopped():
self.timeouthread=Timeout(self.timeouthread.rate)
self.timeouthread.start()
time.sleep(10)
self.timeouthread.setrate(0)
print "2", self.timeouthread.isAlive(),self.timeouthread.isstopped()
if self.timeouthread.isstopped():
self.timeouthread=Timeout(self.timeouthread.rate)
self.timeouthread.start()
time.sleep(10)
self.timeouthread.setrate(3)
print "3", self.timeouthread.isAlive(),self.timeouthread.isstopped()
if self.timeouthread.isstopped():
self.timeouthread=Timeout(self.timeouthread.rate)
self.timeouthread.start()
time.sleep(10)
self.timeouthread.setrate(1)
print "4", self.timeouthread.isAlive(),self.timeouthread.isstopped()
if self.timeouthread.isstopped():
self.timeouthread=Timeout(self.timeouthread.rate)
self.timeouthread.start()
tout=Timeout(2)
tout.start()
Changerate(tout).start()
print "0", threading.enumerate() |
Partager