Python pymongo.errors 模块,ServerSelectionTimeoutError() 实例源码

我们从Python开源项目中,提取了以下29个代码示例,用于说明如何使用pymongo.errors.ServerSelectionTimeoutError()

项目:mongodb-monitoring    作者:jruaux    | 项目源码 | 文件源码
def select_server_by_address(self, address,
                                 server_selection_timeout=None):
        """Return a Server for "address", reconnecting if necessary.

        If the server's type is not known, request an immediate check of all
        servers. Time out after "server_selection_timeout" if the server
        cannot be reached.

        :Parameters:
          - `address`: A (host, port) pair.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value
            common.SERVER_SELECTION_TIMEOUT is used.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        return self.select_server(any_server_selector,
                                  server_selection_timeout,
                                  address)
项目:mongodb_consistent_backup    作者:Percona-Lab    | 项目源码 | 文件源码
def connect(self):
        try:
            logging.debug("Getting MongoDB connection to %s (replicaSet=%s, readPreference=%s, readPreferenceTags=%s, ssl=%s)" % (
                self.uri,
                self.replset,
                self.read_pref,
                self.do_rp_tags,
                self.do_ssl(),
            ))
            conn = MongoClient(**self.client_opts())
            if self.do_connect:
                conn['admin'].command({"ping": 1})
        except (ConnectionFailure, OperationFailure, ServerSelectionTimeoutError), e:
            logging.error("Unable to connect to %s! Error: %s" % (self.uri, e))
            raise DBConnectionError(e)
        if conn is not None:
            self._conn = conn
        return self._conn
项目:CaptsLog    作者:jaehoonhwang    | 项目源码 | 文件源码
def search_entries_by_title(self, title):
        """Search For a Specified Title in the Entries_Table

        Args:
            title(str): the title you are searching for

        Return:
            result(collection): the search result

        """

        entries_table = self.db["Entries_Table"]
        try:
            result = entries_table.find_one({"Title": title})
            return result
        except errors.ServerSelectionTimeoutError:
            print('ERROR : No connection could be made because'
                  ' the target machine actively refused it')
            return False
            # TODO Modify to allow multiple results using find(),
            # TODO also find similar results which are not exact matches
项目:CaptsLog    作者:jaehoonhwang    | 项目源码 | 文件源码
def search_entries_by_created_date(self, date):
        """Search For Entries created on the specified Date in the Entries_Table

        Args:
            date(datetime): the date you are searching for

        Return:
            result(collection): the search result

        """

        if date.date() <= datetime.now().date():
            entries_table = self.db["Entries_Table"]
            try:
                return entries_table.find(
                    {"Date_Created": date})
            except errors.ServerSelectionTimeoutError:
                print('ERROR : No connection could be made because'
                      ' the target machine actively refused it')
                return True
        return False
项目:CaptsLog    作者:jaehoonhwang    | 项目源码 | 文件源码
def search_entries_by_id(self, id):
        """Search For Entries created on the specified Date in the Entries_Table

        Args:
            id(string): the objectID you are searching for

        Return:
            result(collection): the search result

        """

        entries_table = self.db["Entries_Table"]
        try:
            return entries_table.find(
                {"_id": ObjectId(id)})
        except errors.ServerSelectionTimeoutError:
            print('ERROR : No connection could be made because'
                  ' the target machine actively refused it')
            return True
        return False
项目:CaptsLog    作者:jaehoonhwang    | 项目源码 | 文件源码
def search_entries_by_modified_date(self, date):
        """Search For Entries modified on the specified Date in the Entries_Table

        Args:
            date(datetime): the date you are searching for

        Return:
            result(collection): the search result

        """
        if date.date() <= datetime.now().date():
            entries_table = self.db["Entries_Table"]
            try:
                return entries_table.find(
                    {"Last_Modified": date})
            except errors.ServerSelectionTimeoutError:
                print('ERROR : No connection could be made because'
                      ' the target machine actively refused it')
                return True
        return False
项目:CaptsLog    作者:jaehoonhwang    | 项目源码 | 文件源码
def delete_entries(self, _id):
        """Delete entries in the Entries_Table

        Args:
            _id(str):  Object ID of the entry you want to change

        Return:
            result(bool):true if the delete was successful. false if it fails
        """

        entries_table = self.db["Entries_Table"]
        try:
            if not entries_table.find_one({"_id": ObjectId(_id)}):
                # print "The specified entry does not Exist"
                return False
            entries_table.delete_one({"_id": ObjectId(_id)})
            return True
        except errors.ServerSelectionTimeoutError:
            print('ERROR : No connection could be made because'
                  ' the target machine actively refused it')
            return False
项目:CaptsLog    作者:jaehoonhwang    | 项目源码 | 文件源码
def get_all(self):
        """

        Returns:
            result(collection): all elements in the Entries Table
        """

        try:
            entries_table = self.db["Entries_Table"]
            result = entries_table.find()
            print result[0]
            return result
        except errors.ServerSelectionTimeoutError:
            print('ERROR : No connection could be made because'
                  'the target machine actively refused it')
            return None
项目:CaptsLog    作者:jaehoonhwang    | 项目源码 | 文件源码
def support_func_get_all(self, lim):
        """

        Args:
            lim(int): number of items you need to get from the database

        Returns:
            result(collection): all elements in the
        """

        try:
            entries_table = self.db["Entries_Table"]
            result = entries_table.find().limit(lim)
            print result[0]
            return result
        except errors.ServerSelectionTimeoutError:
            print('ERROR : No connection could be made because'
                  'the target machine actively refused it')
            return None
项目:covar_me_app    作者:CovarMe    | 项目源码 | 文件源码
def select_server_by_address(self, address,
                                 server_selection_timeout=None):
        """Return a Server for "address", reconnecting if necessary.

        If the server's type is not known, request an immediate check of all
        servers. Time out after "server_selection_timeout" if the server
        cannot be reached.

        :Parameters:
          - `address`: A (host, port) pair.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value
            common.SERVER_SELECTION_TIMEOUT is used.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        return self.select_server(any_server_selector,
                                  server_selection_timeout,
                                  address)
项目:kekescan    作者:xiaoxiaoleo    | 项目源码 | 文件源码
def select_server_by_address(self, address,
                                 server_selection_timeout=None):
        """Return a Server for "address", reconnecting if necessary.

        If the server's type is not known, request an immediate check of all
        servers. Time out after "server_selection_timeout" if the server
        cannot be reached.

        :Parameters:
          - `address`: A (host, port) pair.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value
            common.SERVER_SELECTION_TIMEOUT is used.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        return self.select_server(any_server_selector,
                                  server_selection_timeout,
                                  address)
项目:flask-zhenai-mongo-echarts    作者:Fretice    | 项目源码 | 文件源码
def select_server_by_address(self, address,
                                 server_selection_timeout=None):
        """Return a Server for "address", reconnecting if necessary.

        If the server's type is not known, request an immediate check of all
        servers. Time out after "server_selection_timeout" if the server
        cannot be reached.

        :Parameters:
          - `address`: A (host, port) pair.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value
            common.SERVER_SELECTION_TIMEOUT is used.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        return self.select_server(any_server_selector,
                                  server_selection_timeout,
                                  address)
项目:Data-visualization    作者:insta-code1    | 项目源码 | 文件源码
def select_server_by_address(self, address,
                                 server_selection_timeout=None):
        """Return a Server for "address", reconnecting if necessary.

        If the server's type is not known, request an immediate check of all
        servers. Time out after "server_selection_timeout" if the server
        cannot be reached.

        :Parameters:
          - `address`: A (host, port) pair.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value
            common.SERVER_SELECTION_TIMEOUT is used.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        return self.select_server(any_server_selector,
                                  server_selection_timeout,
                                  address)
项目:HyperStream    作者:IRC-SPHERE    | 项目源码 | 文件源码
def __init__(self, server_config, auto_connect=True):
        """Initialise the client

        :param server_config: The server configuration
        :param auto_connect: Whether to automatically connect
        """
        self.server_config = server_config

        if auto_connect:
            if ServerSelectionTimeoutError:
                try:
                    self.connect(server_config)
                except ServerSelectionTimeoutError as e:
                    logging.warn(e.message)
                    sys.exit()
            else:
                self.connect(server_config)
项目:hudl-bugbounty    作者:lewislabs    | 项目源码 | 文件源码
def select_server_by_address(self, address,
                                 server_selection_timeout=None):
        """Return a Server for "address", reconnecting if necessary.

        If the server's type is not known, request an immediate check of all
        servers. Time out after "server_selection_timeout" if the server
        cannot be reached.

        :Parameters:
          - `address`: A (host, port) pair.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value
            common.SERVER_SELECTION_TIMEOUT is used.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        return self.select_server(any_server_selector,
                                  server_selection_timeout,
                                  address)
项目:hudl-bugbounty    作者:lewislabs    | 项目源码 | 文件源码
def select_server_by_address(self, address,
                                 server_selection_timeout=None):
        """Return a Server for "address", reconnecting if necessary.

        If the server's type is not known, request an immediate check of all
        servers. Time out after "server_selection_timeout" if the server
        cannot be reached.

        :Parameters:
          - `address`: A (host, port) pair.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value
            common.SERVER_SELECTION_TIMEOUT is used.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        return self.select_server(any_server_selector,
                                  server_selection_timeout,
                                  address)
项目:sc_spider    作者:wings27    | 项目源码 | 文件源码
def save_item(self, item):
        try:
            self.collection.update({'url': item['url']}, dict(item), upsert=True)
        except ServerSelectionTimeoutError as e:
            logging.error('Fail to connect to mongodb. %s', e)
项目:CaptsLog    作者:jaehoonhwang    | 项目源码 | 文件源码
def insert_to_entries_table(self, title, tags, markdown_text):
        """Insert Data into the Entries_Table in the Database

        Args:
            title (str): Title of the Journel Entry
            tags (list): Tags for the Entry
            markdown_text(str): Content of the Entry(Markdown text)

        return:
            result(bool): True for success or False for failure

        """
        if title == str(""):
            # print "Error!! Title Can't be Empty"
            return False
        entry = {"Title": title,
                 "Date_Created": datetime.now(),
                 "Last_Modified": datetime.now(),
                 "Tags": tags,
                 "MarkdownFile": markdown_text}
        t = self.db["Entries_Table"]
        try:
            if t.insert_one(entry):
                return True
        except errors.ServerSelectionTimeoutError:
            print('ERROR : No connection could be made because'
                  ' the target machine actively refused it')
            return True
项目:minion-ci    作者:timofurrer    | 项目源码 | 文件源码
def ensure_mongo(func):
    @wraps(func)
    def func_wrapper(*args, **kwargs):
        client = MongoClient(serverSelectionTimeoutMS=500, connectTimeoutMS=500)
        try:
             # The ismaster command is cheap and does not require auth.
            client.admin.command('ismaster')
        except (errors.ServerSelectionTimeoutError, errors.AutoReconnect):
            raise MinionMongoError("Can't connect to mongodb")
        else:
            return func(*args, **kwargs)
        finally:
            client.close()
    return func_wrapper
项目:deb-python-pysaml2    作者:openstack    | 项目源码 | 文件源码
def test_eptid_mongo_db():
    try:
        edb = EptidMDB("secret", "idp")
    except ConnectionFailure:
        pass
    else:
        try:
            e1 = edb.get("idp_entity_id", "sp_entity_id", "user_id",
                         "some other data")
        except ServerSelectionTimeoutError:
            pass
        else:
            print(e1)
            assert e1.startswith("idp_entity_id!sp_entity_id!")
            e2 = edb.get("idp_entity_id", "sp_entity_id", "user_id",
                         "some other data")
            assert e1 == e2

            e3 = edb.get("idp_entity_id", "sp_entity_id", "user_2",
                         "some other data")
            print(e3)
            assert e1 != e3

            e4 = edb.get("idp_entity_id", "sp_entity_id2", "user_id",
                         "some other data")
            assert e4 != e1
            assert e4 != e3
项目:ieee-crawler    作者:cxsmarkchan    | 项目源码 | 文件源码
def crawl_articles(self, numbers):
        citation_loader = CitationLoader(numbers)
        entries = citation_loader.get_bibtex()
        articles = {}

        for entry in entries:
            number = entry['ID']

            try:
                article = Article.objects.get(entry_number=number)
                logger.info('Article [%s] already exists, it will be updated.' % number)
            except (DoesNotExist, ServerSelectionTimeoutError):
                article = Article()
                article.entry_number = number
                logger.info('Article [%s] is a new article.' % number)

            article.title = entry['title'] if 'title' in entry else ''
            article.author = entry['author'] if 'author' in entry else ''
            article.journal = entry['journal'] if 'journal' in entry else ''
            article.year = entry['year'] if 'year' in entry else ''
            article.volume = entry['volume'] if 'volume' in entry else ''
            article.number = entry['number'] if 'number' in entry else ''
            article.pages = entry['pages'] if 'pages' in entry else ''
            article.abstract = entry['abstract'] if 'abstract' in entry else ''
            article.keyword = entry['keyword'] if 'keyword' in entry else ''
            article.doi = entry['doi'] if 'doi' in entry else ''
            article.issn = entry['issn'] if 'issn' in entry else ''
            article.issue_reference = self.__issue

            try:
                article.save()
                logger.info('Article [%s] saved.' % number)
            except ServerSelectionTimeoutError:
                logger.info('Cannot connect to database, Article [%s] will not be saved.' % number)
            articles[number] = article

        return articles
项目:SuperHoneyPot    作者:TheFixers    | 项目源码 | 文件源码
def receive_data(self, json_dump_in):
        # receives data from the plugins
        result = None
        current_data = json.loads(json_dump_in)
        # data may need to be formatted here, prior to sending to database below

        try:
            honeypot_database_interface.database.clientInfo.insert_one(current_data).inserted_id
        except errors.ServerSelectionTimeoutError:
            self.mail_server.send_mail()
        except errors.NetworkTimeout:
            print('Timed out.')
        return
项目:mongodb-monitoring    作者:jruaux    | 项目源码 | 文件源码
def select_servers(self,
                       selector,
                       server_selection_timeout=None,
                       address=None):
        """Return a list of Servers matching selector, or time out.

        :Parameters:
          - `selector`: function that takes a list of Servers and returns
            a subset of them.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value common.SERVER_SELECTION_TIMEOUT
            is used.
          - `address`: optional server address to select.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        if server_selection_timeout is None:
            server_timeout = self._settings.server_selection_timeout
        else:
            server_timeout = server_selection_timeout

        with self._lock:
            self._description.check_compatible()

            now = _time()
            end_time = now + server_timeout
            server_descriptions = self._apply_selector(selector, address)

            while not server_descriptions:
                # No suitable servers.
                if server_timeout == 0 or now > end_time:
                    raise ServerSelectionTimeoutError(
                        self._error_message(selector))

                self._ensure_opened()
                self._request_check_all()

                # Release the lock and wait for the topology description to
                # change, or for a timeout. We won't miss any changes that
                # came after our most recent _apply_selector call, since we've
                # held the lock until now.
                self._condition.wait(common.MIN_HEARTBEAT_INTERVAL)
                self._description.check_compatible()
                now = _time()
                server_descriptions = self._apply_selector(selector, address)

            return [self.get_server_by_address(sd.address)
                    for sd in server_descriptions]
项目:covar_me_app    作者:CovarMe    | 项目源码 | 文件源码
def select_servers(self,
                       selector,
                       server_selection_timeout=None,
                       address=None):
        """Return a list of Servers matching selector, or time out.

        :Parameters:
          - `selector`: function that takes a list of Servers and returns
            a subset of them.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value common.SERVER_SELECTION_TIMEOUT
            is used.
          - `address`: optional server address to select.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        if server_selection_timeout is None:
            server_timeout = self._settings.server_selection_timeout
        else:
            server_timeout = server_selection_timeout

        with self._lock:
            self._description.check_compatible()

            now = _time()
            end_time = now + server_timeout
            server_descriptions = self._apply_selector(selector, address)

            while not server_descriptions:
                # No suitable servers.
                if server_timeout == 0 or now > end_time:
                    raise ServerSelectionTimeoutError(
                        self._error_message(selector))

                self._ensure_opened()
                self._request_check_all()

                # Release the lock and wait for the topology description to
                # change, or for a timeout. We won't miss any changes that
                # came after our most recent _apply_selector call, since we've
                # held the lock until now.
                self._condition.wait(common.MIN_HEARTBEAT_INTERVAL)
                self._description.check_compatible()
                now = _time()
                server_descriptions = self._apply_selector(selector, address)

            return [self.get_server_by_address(sd.address)
                    for sd in server_descriptions]
项目:kekescan    作者:xiaoxiaoleo    | 项目源码 | 文件源码
def select_servers(self,
                       selector,
                       server_selection_timeout=None,
                       address=None):
        """Return a list of Servers matching selector, or time out.

        :Parameters:
          - `selector`: function that takes a list of Servers and returns
            a subset of them.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value common.SERVER_SELECTION_TIMEOUT
            is used.
          - `address`: optional server address to select.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        if server_selection_timeout is None:
            server_timeout = self._settings.server_selection_timeout
        else:
            server_timeout = server_selection_timeout

        with self._lock:
            self._description.check_compatible()

            now = _time()
            end_time = now + server_timeout
            server_descriptions = self._apply_selector(selector, address)

            while not server_descriptions:
                # No suitable servers.
                if server_timeout == 0 or now > end_time:
                    raise ServerSelectionTimeoutError(
                        self._error_message(selector))

                self._ensure_opened()
                self._request_check_all()

                # Release the lock and wait for the topology description to
                # change, or for a timeout. We won't miss any changes that
                # came after our most recent _apply_selector call, since we've
                # held the lock until now.
                self._condition.wait(common.MIN_HEARTBEAT_INTERVAL)
                self._description.check_compatible()
                now = _time()
                server_descriptions = self._apply_selector(selector, address)

            return [self.get_server_by_address(sd.address)
                    for sd in server_descriptions]
项目:flask-zhenai-mongo-echarts    作者:Fretice    | 项目源码 | 文件源码
def select_servers(self,
                       selector,
                       server_selection_timeout=None,
                       address=None):
        """Return a list of Servers matching selector, or time out.

        :Parameters:
          - `selector`: function that takes a list of Servers and returns
            a subset of them.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value common.SERVER_SELECTION_TIMEOUT
            is used.
          - `address`: optional server address to select.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        if server_selection_timeout is None:
            server_timeout = self._settings.server_selection_timeout
        else:
            server_timeout = server_selection_timeout

        with self._lock:
            self._description.check_compatible()

            now = _time()
            end_time = now + server_timeout
            server_descriptions = self._description.apply_selector(
                selector, address)

            while not server_descriptions:
                # No suitable servers.
                if server_timeout == 0 or now > end_time:
                    raise ServerSelectionTimeoutError(
                        self._error_message(selector))

                self._ensure_opened()
                self._request_check_all()

                # Release the lock and wait for the topology description to
                # change, or for a timeout. We won't miss any changes that
                # came after our most recent apply_selector call, since we've
                # held the lock until now.
                self._condition.wait(common.MIN_HEARTBEAT_INTERVAL)
                self._description.check_compatible()
                now = _time()
                server_descriptions = self._description.apply_selector(
                    selector, address)

            return [self.get_server_by_address(sd.address)
                    for sd in server_descriptions]
项目:Data-visualization    作者:insta-code1    | 项目源码 | 文件源码
def select_servers(self,
                       selector,
                       server_selection_timeout=None,
                       address=None):
        """Return a list of Servers matching selector, or time out.

        :Parameters:
          - `selector`: function that takes a list of Servers and returns
            a subset of them.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value common.SERVER_SELECTION_TIMEOUT
            is used.
          - `address`: optional server address to select.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        if server_selection_timeout is None:
            server_timeout = self._settings.server_selection_timeout
        else:
            server_timeout = server_selection_timeout

        with self._lock:
            self._description.check_compatible()

            now = _time()
            end_time = now + server_timeout
            server_descriptions = self._apply_selector(selector, address)

            while not server_descriptions:
                # No suitable servers.
                if server_timeout == 0 or now > end_time:
                    raise ServerSelectionTimeoutError(
                        self._error_message(selector))

                self._ensure_opened()
                self._request_check_all()

                # Release the lock and wait for the topology description to
                # change, or for a timeout. We won't miss any changes that
                # came after our most recent _apply_selector call, since we've
                # held the lock until now.
                self._condition.wait(common.MIN_HEARTBEAT_INTERVAL)
                self._description.check_compatible()
                now = _time()
                server_descriptions = self._apply_selector(selector, address)

            return [self.get_server_by_address(sd.address)
                    for sd in server_descriptions]
项目:hudl-bugbounty    作者:lewislabs    | 项目源码 | 文件源码
def select_servers(self,
                       selector,
                       server_selection_timeout=None,
                       address=None):
        """Return a list of Servers matching selector, or time out.

        :Parameters:
          - `selector`: function that takes a list of Servers and returns
            a subset of them.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value common.SERVER_SELECTION_TIMEOUT
            is used.
          - `address`: optional server address to select.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        if server_selection_timeout is None:
            server_timeout = self._settings.server_selection_timeout
        else:
            server_timeout = server_selection_timeout

        with self._lock:
            self._description.check_compatible()

            now = _time()
            end_time = now + server_timeout
            server_descriptions = self._apply_selector(selector, address)

            while not server_descriptions:
                # No suitable servers.
                if server_timeout == 0 or now > end_time:
                    raise ServerSelectionTimeoutError(
                        self._error_message(selector))

                self._ensure_opened()
                self._request_check_all()

                # Release the lock and wait for the topology description to
                # change, or for a timeout. We won't miss any changes that
                # came after our most recent _apply_selector call, since we've
                # held the lock until now.
                self._condition.wait(common.MIN_HEARTBEAT_INTERVAL)
                self._description.check_compatible()
                now = _time()
                server_descriptions = self._apply_selector(selector, address)

            return [self.get_server_by_address(sd.address)
                    for sd in server_descriptions]
项目:hudl-bugbounty    作者:lewislabs    | 项目源码 | 文件源码
def select_servers(self,
                       selector,
                       server_selection_timeout=None,
                       address=None):
        """Return a list of Servers matching selector, or time out.

        :Parameters:
          - `selector`: function that takes a list of Servers and returns
            a subset of them.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value common.SERVER_SELECTION_TIMEOUT
            is used.
          - `address`: optional server address to select.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        if server_selection_timeout is None:
            server_timeout = self._settings.server_selection_timeout
        else:
            server_timeout = server_selection_timeout

        with self._lock:
            self._description.check_compatible()

            now = _time()
            end_time = now + server_timeout
            server_descriptions = self._apply_selector(selector, address)

            while not server_descriptions:
                # No suitable servers.
                if server_timeout == 0 or now > end_time:
                    raise ServerSelectionTimeoutError(
                        self._error_message(selector))

                self._ensure_opened()
                self._request_check_all()

                # Release the lock and wait for the topology description to
                # change, or for a timeout. We won't miss any changes that
                # came after our most recent _apply_selector call, since we've
                # held the lock until now.
                self._condition.wait(common.MIN_HEARTBEAT_INTERVAL)
                self._description.check_compatible()
                now = _time()
                server_descriptions = self._apply_selector(selector, address)

            return [self.get_server_by_address(sd.address)
                    for sd in server_descriptions]