开发者

A little misunderstanding timers in python

can somebody tell me how to use this class timers from python in my code more than one time.

import MOD

class timer:
    def __init__(self, seconds):
         self.start(seconds)
    def start(self, seconds):
         self.startTime = MOD.secCounter()
         self.expirationTime = self.startTime + seconds
         if seconds != 0:
            self.running = 1
            self.expired = 0
         else:  
            self.running = 0  
            self.expired = 0
    def stop(self):
         self.running = 0
         self.expired = 0
    def isexpired(self):
         if self.running == 1开发者_运维百科:  
            timeNow = MOD.secCounter()  
            if timeNow > self.expirationTime:    
               self.running = 0    
               self.expired = 1  
            else:    
               self.expired = 0
         return self.expired
    def isrunning(self):
         if self.running == 1:  
             timeNow = MOD.secCounter()  
             if timeNow > self.expirationTime:    
                self.running = 0    
                self.expired = 1  
             else:    
                self.expired = 0
         return self.running
    def change(self, seconds):
         self.expirationTime = self.startTime + seconds
    def count(self):
         if self.running == 1:  
            timeNow = MOD.secCounter()  
            return (timeNow - self.startTime)
         else:  
            return -1

they write this comment:

The following is a simple example about how to use this class:

    import timers
    timerA = timers.timer(0)
    timerA.start(15)
        while 1:  
            if timerA.isexpired():    
               print 'timerA expired'   
               break

but I don't know how to use it more than one time in my code, because I need to use more than one timer in my code,

should I write

    timerB = timers.timer(1)
    timerB.start(1800)
    while 1:  
        if timerB.isexpired():    
        print 'timerA expired'  
        break

any help, please


Close - the argument to timers.timer is the number of seconds that the timer should time for at first. But every time you call timers.timer(), you'll get a new timer instance.

So your code could look more like:

  timerB = timers.timer(1800)
    while 1:  
        if timerB.isexpired():    
        print 'timerA expired'  
        break

Except that this is misleading - timerA and timerB are separate timers, so timerB.isexpired() won't tell you anything about timerA. Maybe you meant it to read "timerB expired"?

And I would also advise against polling timerB.isexpired() so rapidly. Maybe sleep for a second after each check?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜