Beispiel #1
0
 def testPushPop4(self):
     self.assertEquals(TIMEOUT_NEVER, Timeout.current())
     Tasklet.set_current_timeout(10.0)
     self.assertAlmostEqual(10.0, Timeout.current(), places = 1)
     Timeout.push(TIMEOUT_CURRENT)
     self.assertAlmostEqual(10.0, Timeout.current(), places = 1)
     Timeout.pop()
     self.assertAlmostEqual(10.0, Timeout.current(), places = 1)
     Tasklet.set_current_timeout(TIMEOUT_NEVER)
Beispiel #2
0
 def testPushPop4(self):
     self.assertEquals(TIMEOUT_NEVER, Timeout.current())
     Tasklet.set_current_timeout(10.0)
     self.assertAlmostEqual(10.0, Timeout.current(), places=1)
     Timeout.push(TIMEOUT_CURRENT)
     self.assertAlmostEqual(10.0, Timeout.current(), places=1)
     Timeout.pop()
     self.assertAlmostEqual(10.0, Timeout.current(), places=1)
     Tasklet.set_current_timeout(TIMEOUT_NEVER)
Beispiel #3
0
    def testPushPop(self):

        self.assertEquals(TIMEOUT_NEVER, Timeout.current())

        Timeout.push(30)
        self.assertAlmostEqual(30, Timeout.current(), places = 1)
        Timeout.pop()
        self.assertEquals(TIMEOUT_NEVER, Timeout.current())
        Timeout.push(30)
        self.assertAlmostEqual(30, Timeout.current(), places = 1)
        Tasklet.sleep(1.0)
        self.assertAlmostEqual(29, Timeout.current(), places = 1)
        #push a temporary short timeout
        Timeout.push(5)
        self.assertAlmostEqual(5, Timeout.current(), places = 1)
        Timeout.pop()
        self.assertAlmostEqual(29, Timeout.current(), places = 1)

        #try to push a new longer timeout than the parent timeout
        #this should fail, e.g. it will keep the parent timeout
        Timeout.push(60)
        self.assertAlmostEqual(29, Timeout.current(), places = 1)
        Timeout.pop()
        self.assertAlmostEqual(29, Timeout.current(), places = 1)
        Timeout.pop()
        self.assertEquals(TIMEOUT_NEVER, Timeout.current())
Beispiel #4
0
    def testPushPop(self):

        self.assertEquals(TIMEOUT_NEVER, Timeout.current())

        Timeout.push(30)
        self.assertAlmostEqual(30, Timeout.current(), places=1)
        Timeout.pop()
        self.assertEquals(TIMEOUT_NEVER, Timeout.current())
        Timeout.push(30)
        self.assertAlmostEqual(30, Timeout.current(), places=1)
        Tasklet.sleep(1.0)
        self.assertAlmostEqual(29, Timeout.current(), places=1)
        #push a temporary short timeout
        Timeout.push(5)
        self.assertAlmostEqual(5, Timeout.current(), places=1)
        Timeout.pop()
        self.assertAlmostEqual(29, Timeout.current(), places=1)

        #try to push a new longer timeout than the parent timeout
        #this should fail, e.g. it will keep the parent timeout
        Timeout.push(60)
        self.assertAlmostEqual(29, Timeout.current(), places=1)
        Timeout.pop()
        self.assertAlmostEqual(29, Timeout.current(), places=1)
        Timeout.pop()
        self.assertEquals(TIMEOUT_NEVER, Timeout.current())
Beispiel #5
0
    def connect(self):
        """get a connection from the pool, will wait for maxWaitTime for connection to become
        available, or will create a new connection if connectioncount < max_connections"""
        with Timeout.push(self._connect_timeout):
            if (not self._pool) and (self.connection_count <
                                     self._max_connections):
                #none available, but still allowed to create new connection
                try:
                    return (True, self._new())
                except TimeoutError:
                    raise
                except Exception:
                    self.log.exception(
                        "%s: could not create new connection for pool", self)
                    #we will continue from here waiting for idle connection

            #if we are here, either connection is available, not available but no more connections are allowed,
            #or there was some exception creating a new connection
            self.log.debug("waiting for connection")
            with self._queue_wait_timer_statistic.time():
                #keep track off the amount of other tasks waiting for a connection
                balance = self._pool.channel.balance
                waiters = -balance if balance < 0 else 0
                self._queue_wait_tasks_statistic.set_count(waiters)
                self._queue_wait_tasks_statistic.update_avg(waiters)
                connection = self._get_connection_from_pool()
                self.log.debug("got connection")
                return (False, connection)
Beispiel #6
0
    def connect(self):
        """get a connection from the pool, will wait for maxWaitTime for connection to become
        available, or will create a new connection if connectioncount < max_connections"""
        with Timeout.push(self._connect_timeout):
            if (not self._pool) and (self.connection_count < self._max_connections):
                #none available, but still allowed to create new connection
                try:
                    return (True, self._new())
                except TaskletExit:
                    raise #server exiting
                except TimeoutError:
                    raise
                except:
                    self.log.exception("%s: could not create new connection for pool", self)
                    #we will continue from here waiting for idle connection

            #if we are here, either connection is available, not available but no more connections are allowed,
            #or there was some exception creating a new connection
            self.log.debug("waiting for connection")
            with self._queue_wait_timer_statistic.time():
                #keep track off the amount of other tasks waiting for a connection
                balance = self._pool.channel.balance
                waiters = -balance if balance < 0 else 0
                self._queue_wait_tasks_statistic.set_count(waiters)
                self._queue_wait_tasks_statistic.update_avg(waiters)
                connection = self._get_connection_from_pool()
                self.log.debug("got connection")
                return (False, connection)
Beispiel #7
0
    def testTimer(self):

        ch = Channel()

        def sender(times):
            for i in range(times):
                Tasklet.sleep(1.0)
                ch.send(True)

        with Timeout.push(10):
            Tasklet.new(sender)(4)
            for i in range(4):
                ch.receive(Timeout.current())

        start = time.time()
        try:
            with Timeout.push(2.5):
                Tasklet.new(sender)(4)
                for i in range(4):
                    ch.receive(Timeout.current())
                self.fail('expected timeout')
        except TimeoutError, e:
            end = time.time()
            self.assertAlmostEqual(2.5, end - start, places=1)
Beispiel #8
0
    def testTimer(self):

        ch = Channel()

        def sender(times):
            for i in range(times):
                Tasklet.sleep(1.0)
                ch.send(True)

        with Timeout.push(10):
            Tasklet.new(sender)(4)
            for i in range(4):
                ch.receive(Timeout.current())

        start = time.time()
        try:
            with Timeout.push(2.5):
                Tasklet.new(sender)(4)
                for i in range(4):
                    ch.receive(Timeout.current())
                self.fail('expected timeout')
        except TimeoutError, e:
            end = time.time()
            self.assertAlmostEqual(2.5, end - start, places = 1)
Beispiel #9
0
    def testMySQLTimeout(self):
        cnn = client.connect(host = DB_HOST, user = DB_USER,
                             passwd = DB_PASSWD, db = DB_DB)

        rs = cnn.query("select sleep(2)")
        list(rs)
        rs.close()

        from concurrence import TimeoutError
        from concurrence.timer import Timeout

        start = time.time()
        try:
            with Timeout.push(2.0):
                cnn.query("select sleep(4)")
                self.fail('expected timeout')
        except TimeoutError, e:
            end = time.time()
            self.assertAlmostEqual(2.0, end - start, places = 1)
Beispiel #10
0
    def testMySQLTimeout(self):
        cnn = client.connect(host = DB_HOST, user = DB_USER, 
                             passwd = DB_PASSWD, db = DB_DB)

        rs = cnn.query("select sleep(2)")
        list(rs)
        rs.close()
        
        from concurrence import TimeoutError
        from concurrence.timer import Timeout

        start = time.time()
        try:
            with Timeout.push(2.0):
                cnn.query("select sleep(4)")
                self.fail('expected timeout')
        except TimeoutError, e:
            end = time.time()
            self.assertAlmostEqual(2.0, end - start, places = 1)
Beispiel #11
0
 def loop(self):
     "Infinitely read serial input and parse packets"
     with self.stream.get_reader() as reader:
         while True:
             try:
                 # wait for preamble
                 while True:
                     data = ord(reader.read_bytes(1))
                     if data == 0x5a:
                         break
                 # read packet length
                 _crc = 0
                 pktlen = ord(reader.read_bytes(1))
                 _crc = crc(_crc, pktlen)
                 # read packet data
                 if pktlen > 0:
                     data = []
                     try:
                         with Timeout.push(1):
                             for d in reader.read_bytes(pktlen):
                                 d = ord(d)
                                 data.append(d)
                                 _crc = crc(_crc, d)
                             # read CRC
                             _crc = crc(_crc, ord(reader.read_bytes(1)))
                     except TimeoutError:
                         pass
                     else:
                         # check CRC
                         if _crc == 0:
                             #print "%s recv: %s" % (self._devname, ", ".join(["0x%02x" % d for d in data]))
                             if self._calibrated:
                                 if len(data) == 1 and (data[0] == ord('I')
                                                        or data[0] == 0xf2):
                                     self._calibrated = False
                                     Tasklet.new(
                                         self.auto_calibrate_baudrate)()
                             if self._calibrated or data[0] == 0xf0 or data[
                                     0] == ord('E'):
                                 self.dispatcher.receive(data)
             except Exception as e:
                 logging.exception(e)
Beispiel #12
0
    def testPushPop2(self):

        self.assertEquals(-1, Timeout.current())
        Timeout.push(-1)
        self.assertEquals(-1, Timeout.current())
        Timeout.pop()
        self.assertEquals(-1, Timeout.current())

        Timeout.push(10)
        self.assertAlmostEqual(10, Timeout.current(), places=1)
        Timeout.push(5)
        self.assertAlmostEqual(5, Timeout.current(), places=1)
        Timeout.pop()
        self.assertAlmostEqual(10, Timeout.current(), places=1)
        Timeout.pop()
        self.assertEquals(-1, Timeout.current())
Beispiel #13
0
 def testPushPop2(self):
     
     self.assertEquals(-1, Timeout.current())
     Timeout.push(-1)
     self.assertEquals(-1, Timeout.current())
     Timeout.pop()
     self.assertEquals(-1, Timeout.current())
     
     Timeout.push(10)
     self.assertAlmostEqual(10, Timeout.current(), places = 1)        
     Timeout.push(5)
     self.assertAlmostEqual(5, Timeout.current(), places = 1)
     Timeout.pop()
     self.assertAlmostEqual(10, Timeout.current(), places = 1)
     Timeout.pop()
     self.assertEquals(-1, Timeout.current())
Beispiel #14
0
    def testPushPop2(self):

        self.assertEquals(TIMEOUT_NEVER, Timeout.current())
        Timeout.push(TIMEOUT_NEVER)
        self.assertEquals(TIMEOUT_NEVER, Timeout.current())
        Timeout.pop()
        self.assertEquals(TIMEOUT_NEVER, Timeout.current())

        Timeout.push(10)
        self.assertAlmostEqual(10, Timeout.current(), places=1)
        Timeout.push(5)
        self.assertAlmostEqual(5, Timeout.current(), places=1)
        Timeout.pop()
        self.assertAlmostEqual(10, Timeout.current(), places=1)
        Timeout.pop()
        self.assertEquals(TIMEOUT_NEVER, Timeout.current())
Beispiel #15
0
    def testPushPop2(self):

        self.assertEquals(TIMEOUT_NEVER, Timeout.current())
        Timeout.push(TIMEOUT_NEVER)
        self.assertEquals(TIMEOUT_NEVER, Timeout.current())
        Timeout.pop()
        self.assertEquals(TIMEOUT_NEVER, Timeout.current())

        Timeout.push(10)
        self.assertAlmostEqual(10, Timeout.current(), places = 1)
        Timeout.push(5)
        self.assertAlmostEqual(5, Timeout.current(), places = 1)
        Timeout.pop()
        self.assertAlmostEqual(10, Timeout.current(), places = 1)
        Timeout.pop()
        self.assertEquals(TIMEOUT_NEVER, Timeout.current())
Beispiel #16
0
 def loop(self):
     "Infinitely read serial input and parse packets"
     with self.stream.get_reader() as reader:
         while True:
             try:
                 # wait for preamble
                 while True:
                     data = ord(reader.read_bytes(1))
                     if data == 0x5a:
                         break
                 # read packet length
                 _crc = 0
                 pktlen = ord(reader.read_bytes(1))
                 _crc = crc(_crc, pktlen)
                 # read packet data
                 if pktlen > 0:
                     data = []
                     try:
                         with Timeout.push(1):
                             for d in reader.read_bytes(pktlen):
                                 d = ord(d)
                                 data.append(d)
                                 _crc = crc(_crc, d)
                             # read CRC
                             _crc = crc(_crc, ord(reader.read_bytes(1)))
                     except TimeoutError:
                         pass
                     else:
                         # check CRC
                         if _crc == 0:
                             #print "%s recv: %s" % (self._devname, ", ".join(["0x%02x" % d for d in data]))
                             if self._calibrated:
                                 if len(data) == 1 and (data[0] == ord('I') or data[0] == 0xf2):
                                     self._calibrated = False
                                     Tasklet.new(self.auto_calibrate_baudrate)()
                             if self._calibrated or data[0] == 0xf0 or data[0] == ord('E'):
                                 self.dispatcher.receive(data)
             except Exception as e:
                 logging.exception(e)
Beispiel #17
0
 def read_request(self, reader):
     with Timeout.push(self._server.read_timeout):
         self._read_request(reader)
Beispiel #18
0
 def read_request(self, reader):
     with Timeout.push(self._server.read_timeout):
         self._read_request(reader)
Beispiel #19
0
 def __call__(self, next, *args, **kwargs):
     timeout = float(self.request.environ.get('HTTP_TIMEOUT', '-1'))
     with Timeout.push(timeout):
         return next(*args, **kwargs)
Beispiel #20
0
 def __call__(self, next, *args, **kwargs):
     timeout = float(self.request.environ.get('HTTP_TIMEOUT', '-1'))
     with Timeout.push(timeout):
         return next(*args, **kwargs)
Beispiel #21
0
 def connect(self):
     self.log.debug("connect: %s", self)
     with Timeout.push(self._connect_timeout):
         return (True, self._new())
Beispiel #22
0
 def connect(self):
     self.log.debug("connect: %s", self)
     with Timeout.push(self._connect_timeout):
         return (True, self._new())