当前位置: 首页>>代码示例>>Python>>正文


Python Connection.end_request方法代码示例

本文整理汇总了Python中pymongo.connection.Connection.end_request方法的典型用法代码示例。如果您正苦于以下问题:Python Connection.end_request方法的具体用法?Python Connection.end_request怎么用?Python Connection.end_request使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pymongo.connection.Connection的用法示例。


在下文中一共展示了Connection.end_request方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_connection

# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import end_request [as 别名]
    def test_connection(self):
        c = Connection(host, port)
        self.assertTrue(c.auto_start_request)
        self.assertEqual(None, c.max_pool_size)
        self.assertFalse(c.slave_okay)
        self.assertFalse(c.safe)
        self.assertEqual({}, c.get_lasterror_options())

        # Connection's writes are unacknowledged by default
        doc = {"_id": ObjectId()}
        coll = c.pymongo_test.write_concern_test
        coll.drop()
        coll.insert(doc)
        coll.insert(doc)

        c = Connection("mongodb://%s:%s/?safe=true" % (host, port))
        self.assertTrue(c.safe)

        # To preserve legacy Connection's behavior, max_size should be None.
        # Pool should handle this without error.
        self.assertEqual(None, get_pool(c).max_size)
        c.end_request()

        # Connection's network_timeout argument is translated into
        # socketTimeoutMS
        self.assertEqual(123, Connection(
            host, port, network_timeout=123)._MongoClient__net_timeout)

        for network_timeout in 'foo', 0, -1:
            self.assertRaises(
                ConfigurationError,
                Connection, host, port, network_timeout=network_timeout)
开发者ID:Bloodevil,项目名称:mongo-python-driver,代码行数:34,代码来源:test_legacy_connections.py

示例2: test_copy_db

# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import end_request [as 别名]
    def test_copy_db(self):
        c = Connection(self.host, self.port)
        self.assertTrue(c.in_request())

        self.assertRaises(TypeError, c.copy_database, 4, "foo")
        self.assertRaises(TypeError, c.copy_database, "foo", 4)

        self.assertRaises(InvalidName, c.copy_database, "foo", "$foo")

        c.pymongo_test.test.drop()
        c.drop_database("pymongo_test1")
        c.drop_database("pymongo_test2")

        c.pymongo_test.test.insert({"foo": "bar"})

        # Due to SERVER-2329, databases may not disappear from a master in a
        # master-slave pair
        if not server_is_master_with_slave(c):
            self.assertFalse("pymongo_test1" in c.database_names())
            self.assertFalse("pymongo_test2" in c.database_names())

        c.copy_database("pymongo_test", "pymongo_test1")
        # copy_database() didn't accidentally end the request
        self.assertTrue(c.in_request())

        self.assertTrue("pymongo_test1" in c.database_names())
        self.assertEqual("bar", c.pymongo_test1.test.find_one()["foo"])

        c.end_request()
        self.assertFalse(c.in_request())
        c.copy_database("pymongo_test", "pymongo_test2", "%s:%d" % (self.host, self.port))
        # copy_database() didn't accidentally restart the request
        self.assertFalse(c.in_request())

        self.assertTrue("pymongo_test2" in c.database_names())
        self.assertEqual("bar", c.pymongo_test2.test.find_one()["foo"])

        if version.at_least(c, (1, 3, 3, 1)):
            c.drop_database("pymongo_test1")

            c.pymongo_test.add_user("mike", "password")

            self.assertRaises(
                OperationFailure, c.copy_database, "pymongo_test", "pymongo_test1", username="foo", password="bar"
            )
            if not server_is_master_with_slave(c):
                self.assertFalse("pymongo_test1" in c.database_names())

            self.assertRaises(
                OperationFailure, c.copy_database, "pymongo_test", "pymongo_test1", username="mike", password="bar"
            )

            if not server_is_master_with_slave(c):
                self.assertFalse("pymongo_test1" in c.database_names())

            if not is_mongos(c):
                # See SERVER-6427
                c.copy_database("pymongo_test", "pymongo_test1", username="mike", password="password")
                self.assertTrue("pymongo_test1" in c.database_names())
                self.assertEqual("bar", c.pymongo_test1.test.find_one()["foo"])
开发者ID:nickmilon,项目名称:mongo-python-driver,代码行数:62,代码来源:test_connection.py

示例3: test_end_request

# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import end_request [as 别名]
    def test_end_request(self):
        self.skip()
        connection = Connection([self.left, self.right])
        db = connection.pymongo_test

        for _ in range(100):
            db.test.remove({})
            db.test.insert({})
            self.assertTrue(db.test.find_one())
            connection.end_request()
开发者ID:cathoderay,项目名称:mongo-python-driver,代码行数:12,代码来源:test_paired.py

示例4: PyMongo

# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import end_request [as 别名]
class PyMongo(object):
    """This class is used to control the pymongo integration with a Flask
    application. There are two usage modes which are similar.

    The first usage mode is to bind the instance to a specific application::

        app = Flask(__name__)
        db = PyMongo(app)

    The second usage mode is to initialize the extension and provide an
    application object later::

        db = PyMongo()

        def create_app():
          app = Flask(__name__)
          db.init_app(app)
          return app

    The latter of course has the benefit of avoiding all kinds of problems as
    described in the Flask documentation on the :ref:`~app-factories` pattern.

    During initialization, PyMongo takes another optional parameter `database`
    that can be used to set a the name of the default database to be used for
    all models that do not provide a `database` attribute themselves. This is
    often useful as one database is all that is used in many applications.

    This class also provides access to the pymongo constants for indexing and
    profiling.
    """

    def __init__(self, app=None, database=None, **kwargs):
        self.database = database
        self.Model = Model
        self.Model.query = self
        self.Query = Query
        self.Index = Index

        self._include_constants()

        if app is not None:
            self.init_app(app, **kwargs)
        else:
            self.app = None
            self.hosts = None
    
    def init_app(self, app, **kwargs):
        """Initializes `app`, a :class:`~flask.Flask` application, for use with
        the specified configuration variables. Keyword arguments passed to this
        override the configuration options.
        """
        options = {
            'max_pool_size': app.config.get('MONGO_MAX_POOL_SIZE', 10),
            'network_timeout': app.config.get('MONGO_NETWORK_TIMEOUT', None),
            'tz_aware': app.config.get('MONGO_TZ_AWARE', False),
            'slave_okay': app.config.get('MONGO_SLAVE_OKAY', False),
            'safe': app.config.get('MONGO_GETLASTERROR', False),
            'fsync': app.config.get('MONGO_GETLASTERROR_FSYNC', None),
            'j': app.config.get('MONGO_GETLASTERROR_J', None),
            'w': app.config.get('MONGO_GETLASTERROR_W', None),
            'wtimeout': app.config.get('MONGO_GETLASTERROR_W_TIMEOUT', None),
            'replicaset': app.config.get('MONGO_REPLICA_SET', None),
        }.update(kwargs)

        self.app = app
        self.hosts = app.config.get('MONGO_HOSTS', "mongodb://localhost:27017")
        self.connection = BaseConnection(self.hosts, options)

        @app.teardown_request
        def free_sockets(response):
            # release thread connection to pool so socket is reclaimed
            self.connection.end_request()
            return response

        for model, indices in _indices.iteritems():
            for index in indices:
                index.ensure(model.query)

    def _include_constants(self):
        self.ASCENDING = ASCENDING
        self.DESCENDING = DESCENDING
        self.GEO2D = GEO2D

    def __repr__(self):
        return "<%s connection=%s>" % (self.__class__.__name__, self.hosts)
开发者ID:pnelson,项目名称:flask-pymongo,代码行数:87,代码来源:flask_pymongo.py


注:本文中的pymongo.connection.Connection.end_request方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。