From 77a1d8abdd026a28b143d8bab8228be473bdb0ab Mon Sep 17 00:00:00 2001 From: Luyun Xie Date: Mon, 5 Dec 2016 22:36:45 +0800 Subject: [PATCH] add ConnectionPool.autoconnect --- happybase/pool.py | 17 +++++++++++------ tests/test_api.py | 4 ++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/happybase/pool.py b/happybase/pool.py index 2d6fee2..95867f5 100644 --- a/happybase/pool.py +++ b/happybase/pool.py @@ -48,10 +48,14 @@ class ConnectionPool(object): task of the pool. :param int size: the maximum number of concurrently open connections + :param bool autoconnect: Whether a connection should be created upon + instantiation to test connection availability. + Note: the `autoconnect` flag of Connection instance inside the pool + is always False. :param kwargs: keyword arguments passed to :py:class:`happybase.Connection` """ - def __init__(self, size, **kwargs): + def __init__(self, size, autoconnect=True, **kwargs): if not isinstance(size, int): raise TypeError("Pool 'size' arg must be an integer") @@ -72,11 +76,12 @@ def __init__(self, size, **kwargs): connection = Connection(**connection_kwargs) self._queue.put(connection) - # The first connection is made immediately so that trivial - # mistakes like unresolvable host names are raised immediately. - # Subsequent connections are connected lazily. - with self.connection(): - pass + if autoconnect: + # The first connection is made immediately so that trivial + # mistakes like unresolvable host names are raised immediately. + # Subsequent connections are connected lazily. + with self.connection(): + pass def _acquire_connection(self, timeout=None): """Acquire a connection from the pool.""" diff --git a/tests/test_api.py b/tests/test_api.py index 642bb0d..6a5ce97 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -16,6 +16,7 @@ assert_false, assert_in, assert_is_instance, + assert_is_none, assert_is_not_none, assert_list_equal, assert_not_in, @@ -487,6 +488,9 @@ def test_connection_pool_construction(): with assert_raises(ValueError): ConnectionPool(size=0) + pool = ConnectionPool(size=1, autoconnect=False) + assert_is_none(getattr(pool._thread_connections, 'current')) + def test_connection_pool():