Exemplo n.º 1
0
 def run( self, timeout=None ) :
     self.stopped = False
     deadline = None
     if timeout is not None :
         deadline = seconds() + timeout
     try :
         while 1 :
             if (deadline is not None) and (seconds() >= deadline) :
                 break
             self.runOnce()
     except SelectStoppedError :
         return
     except KeyboardInterrupt :
         print 'Ctrl-C detected'
         return
Exemplo n.º 2
0
 def run(self, timeout=None):
     self.stopped = False
     deadline = None
     if timeout is not None:
         deadline = seconds() + timeout
     try:
         while 1:
             if (deadline is not None) and (seconds() >= deadline):
                 break
             self.runOnce()
     except SelectStoppedError:
         return
     except KeyboardInterrupt:
         print 'Ctrl-C detected'
         return
Exemplo n.º 3
0
 def run( self, timeout=None ) :
     global logger
     self.stopped = False
     deadline = None
     if timeout is not None :
         deadline = seconds() + timeout
     try :
         while 1 :
             if (deadline is not None) and (seconds() >= deadline) :
                 break
             self.runOnce()
     except SelectStoppedError :
         logger.warn('reactor stopped')
         return
     except KeyboardInterrupt :
         logger.warn('Ctrl-C detected')
         return
Exemplo n.º 4
0
    def runOnce(self):
        timeout = 0.1
        now = seconds()
        dl = self.deadlineList
        if dl and (timeout + now > dl[0][0]):
            timeout = dl[0][0] - now
            if timeout < 0: timeout = 0

        if self.stopped:
            raise SelectStoppedError
        (rs, ws, es) = _select(self.r.keys(), self.w.keys(), self.e.keys(),
                               timeout)

        now = seconds()
        fired = []
        for (deadline, ti) in dl:
            if deadline <= now:
                fired.append(ti)
            else:
                break
        if fired: del dl[0:len(fired)]
        for ti in fired:
            if ti.timerId in self.timers:
                if ti.singleShot:
                    del self.timers[ti.timerId]
                try:
                    ti.callback()
                except:
                    logger.exception('Error in timer callback')
                    if self._failOnException: raise
                if (not ti.singleShot) and (ti.timerId in self.timers):
                    ti.deadline = now + ti.timeout
                    insort(dl, (ti.deadline, ti))

        for (fired, map) in ((rs, self.r), (ws, self.w), (es, self.e)):
            for sockfd in fired:
                cb = map.get(sockfd, None)
                if cb is not None:
                    try:
                        cb()
                    except:
                        logger.exception('Error in socket event handler')
                        if self._failOnException: raise
                    if self.stopped:
                        raise SelectStoppedError
Exemplo n.º 5
0
    def runOnce( self ) :
        global logger
        timeout = 0.1
        now = seconds()
        dl = self.deadlineList
        if dl and (timeout+now > dl[0][0]) :
            timeout = dl[0][0] - now
            if timeout < 0 : timeout = 0

        if self.stopped :
            raise SelectStoppedError
        (rs, ws, es) = _select( self.r.keys(), self.w.keys(), self.e.keys(), timeout )

        now = seconds()
        fired = []
        for (deadline,ti) in dl :
            if deadline <= now :
                fired.append(ti)
            else : break
        if fired : del dl[0:len(fired)]
        for ti in fired :
            if ti.timerId in self.timers :
                if ti.singleShot :
                    del self.timers[ti.timerId]
                try :
                    ti.callback()
                except :
                    logger.exception( 'Error in timer callback' )
                    if self._failOnException : raise
                if (not ti.singleShot) and (ti.timerId in self.timers) :
                    ti.deadline = now + ti.timeout
                    insort( dl, (ti.deadline,ti) )

        for (fired,map) in ((rs,self.r), (ws,self.w), (es,self.e)) :
            for sockfd in fired :
                cb = map.get( sockfd, None )
                if cb is not None :
                    try :
                        cb()
                    except :
                        logger.exception( 'Error in socket event handler' )
                        if self._failOnException : raise
                    if self.stopped :
                        raise SelectStoppedError
Exemplo n.º 6
0
 def __init__( self, timerId, timeout, callback, singleShot ) :
     self.timerId = timerId
     self.timeout = timeout
     self.callback = callback
     self.singleShot = singleShot
     self.deadline = seconds() + timeout
Exemplo n.º 7
0
 def __init__(self, timerId, timeout, callback, singleShot):
     self.timerId = timerId
     self.timeout = timeout
     self.callback = callback
     self.singleShot = singleShot
     self.deadline = seconds() + timeout