connection_pool.py 文件源码

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

项目:kingpin 作者: pinterest 项目源码 文件源码
def get(self, block=True, timeout=None):
        """Get a connection holder with connection object (conn) populated.

        Args:
            block: whether to wait if queue is empty.
            timeout: the max seconds to wait. If no connection is available
            after timeout, a gevent.queue.Empty exception is thrown.

        Returns:
            a ConnectionHolder object with conn populated.

        """
        conn_holder = self._queue.get(block, timeout)
        if conn_holder.conn is None:
            tm = None
            try:
                # In case self._create_conn() blocks, it should block for max
                # timeout seconds.
                tm = gevent.Timeout.start_new(timeout, gevent.queue.Empty)
                conn_holder.set_conn(self._create_conn())
            except:
                # If we fail to create a connection, we put conn_holder back
                # and re-raise the exception.
                conn_holder.set_conn(None)
                self.put(conn_holder)
                raise
            finally:
                if tm:
                    tm.cancel()

            self.num_connected += 1

        conn_holder.last_access_time = time.time()
        return conn_holder
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号