def __init__(self, connector, dbargs, max_connections=10, connect_timeout=-1, max_connection_age=None, max_connection_age_reaper_interval=60): super(Pool, self).__init__(connector, dbargs, connect_timeout) self._max_connections = max_connections self._max_connection_age = max_connection_age #some statistics self._queue_wait_timer_statistic = StatisticExtra() self._queue_wait_tasks_statistic = StatisticExtra() self._pool = Deque() #the pool of available idle connections #watch for server disconnects on idle connections: self._idle_disconnect_channel = Channel() self._idle_disconnect_reaper_task = Tasklet.loop( self._idle_disconnect_reaper, daemon=True)() #check for old connections if self._max_connection_age is not None: self._old_connection_reaper_task = Tasklet.interval( max_connection_age_reaper_interval, self._old_connection_reaper, daemon=True)()
def serve(self): """listens and starts a new tasks accepting incoming connections on the configured address""" if self._socket is None: self.bind() self.listen() if not callable(self._handler): assert False, "handler not set or not callable" self._accept_task = Tasklet.loop(self._accept_task_loop, name = self._accept_task_name, daemon = True)()
def __init__(self, connector, dbargs, max_connections = 10, connect_timeout = -1, max_connection_age = None, max_connection_age_reaper_interval = 60): super(Pool, self).__init__(connector, dbargs, connect_timeout) self._max_connections = max_connections self._max_connection_age = max_connection_age #some statistics self._queue_wait_timer_statistic = StatisticExtra() self._queue_wait_tasks_statistic = StatisticExtra() self._pool = Deque() #the pool of available idle connections #watch for server disconnects on idle connections: self._idle_disconnect_channel = Channel() self._idle_disconnect_reaper_task = Tasklet.loop(self._idle_disconnect_reaper, daemon = True)() #check for old connections if self._max_connection_age is not None: self._old_connection_reaper_task = Tasklet.interval(max_connection_age_reaper_interval, self._old_connection_reaper, daemon = True)()
def testLoop(self): recvd = [] def looper(channel): res = channel.receive() if res == None: raise Exception( "this is an expected exception!! (not a failed test...)") else: recvd.append(res) looper_channel = Channel() looper_task = Tasklet.loop(looper)(looper_channel) for i in range(10): looper_channel.send(i) self.assertEqual(range(10), recvd) self.assertEqual(-1, looper_channel.balance) self.assertTrue(looper_task.alive) looper_channel.send(None) #will trigger exception loop #must still be working recvd = [] for i in range(10): looper_channel.send(i) self.assertEqual(range(10), recvd) self.assertEqual(-1, looper_channel.balance) looper_task.kill() self.assertEqual(0, looper_channel.balance) #assert that looper exitted, because it is not receiving anymore self.assertFalse(looper_channel.has_receiver()) self.assertFalse(looper_task.alive)
def testLoop(self): recvd = [] def looper(channel): res = channel.receive() if res == None: raise Exception("some exception") else: recvd.append(res) looper_channel = Channel() looper_task = Tasklet.loop(looper)(looper_channel) for i in range(10): looper_channel.send(i) self.assertEqual(range(10), recvd) self.assertEqual(-1, looper_channel.balance) self.assertTrue(looper_task.alive) looper_channel.send(None) #will trigger exception loop #must still be working recvd = [] for i in range(10): looper_channel.send(i) self.assertEqual(range(10), recvd) self.assertEqual(-1, looper_channel.balance) looper_task.kill() self.assertEqual(0, looper_channel.balance) #assert that looper exitted, because it is not receiving anymore self.assertFalse(looper_channel.has_receiver()) self.assertFalse(looper_task.alive)