connection_pool.py 文件源码

python
阅读 33 收藏 0 点赞 0 评论 0

项目:kingpin 作者: pinterest 项目源码 文件源码
def __init__(self, pool_name, pool_size, close_conn_f, conn_cls,
                 *conn_args, **conn_kwargs):
        """Constructor.

        Args:
            pool_name: name of the pool.
            pool_size: max number of connections to create in the pool.
            close_conn_f: function to close a connection. It should take
            exactly one argument which is an object returned by conn_cls.
            conn_cls: python class or function for creating a connection.
            conn_args, conn_kwargs: arguments passed to conn_cls to
            create a connection.

        """
        self.pool_name = pool_name
        self.pool_size = pool_size
        assert close_conn_f is None or hasattr(close_conn_f, '__call__')
        self.close_conn_f = close_conn_f
        assert hasattr(conn_cls, '__call__')
        self.conn_cls = conn_cls
        self.conn_args = conn_args
        self.conn_kwargs = conn_kwargs
        # The number of connections in the pool that are ever used,
        # e.g. total unique number of connections returned by get().
        # This is the maximum number of concurrent connections ever reached.
        self.num_connected = 0

        self._queue = gevent.queue.LifoQueue(maxsize=pool_size)

        for i in xrange(0, pool_size):
            # Pre-populate the pool with connection holders.
            self._queue.put(ConnectionHolder(pool_name))

        # Run garbage collection on unused connections.
        # Randomize the GC job start time.
        start_after_secs = random.randint(0, 1000 * GC_INTERVAL_SECS) / 1000.0
        self._gc_job = Periodical("ConnPool-GC-%s" % pool_name,
                                  GC_INTERVAL_SECS, start_after_secs,
                                  self._gc_unused_conn, MAX_CONN_AGE_SECS)

        self.desc = self._get_desc()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号