def get_socket(self, host, port): pid = os.getpid() if pid != self._pid: self._bootstrap(pid) greenlet = getcurrent() from_pool = True sock = self._used.get(greenlet) if sock is None: with self._lock: if self._count < self.pool_size: self._count += 1 from_pool = False sock = self.connect(host, port) if sock is None: from_pool = True sock = self._queue.get(timeout=self.network_timeout) if isinstance(greenlet, gevent.Greenlet): greenlet.link(self._return) self._used[greenlet] = sock else: ref = weakref.ref(greenlet, self._return) self._used[ref] = sock return sock, from_pool
def get(self): greenlet = gevent.hub.getcurrent() ref = self._reference_or_greenlet() conn = self._used.get(ref) if conn is None: with self._lock: # Prefer an idle connection from the pool over creating connections to pool limit. if len(self._used) < self._count: conn = self._queue.get(timeout=self.internal_timeout) if conn is None and self._count < self.pool_size: self._count += 1 conn = self._make_connection() if conn is None: conn = self._queue.get(timeout=self.network_timeout) if isinstance(greenlet, gevent.Greenlet): greenlet.link(self._put) self._used[ref] = conn return conn