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
评论列表
文章目录