zoukankan      html  css  js  c++  java
  • pymongo中的连接操作:Connection()与MongoClient()

    class MongoClient(pymongo.common.BaseObject)
       Connection to MongoDB.
     
       Method resolution order:
           MongoClient
           pymongo.common.BaseObject
           __builtin__.object
    


    class Connection(pymongo.mongo_client.MongoClient)
       Connection to MongoDB.
     
       Method resolution order:
           Connection
           pymongo.mongo_client.MongoClient
           pymongo.common.BaseObject
           __builtin__.object


    我们先看一下源代码,从这两个类的继承来看,connection是继承了MongoClient的,建议使用MongoClient而不是使用Connection。

    (也就是说,MongoClient能够用法Connection都能够使用)


    from pymongo import MongoClient
    client = MongoClient('192.168.40.87', 27037)
    db_name = 'TCL_Useraction'
    db = client[db_name]
    collection_useraction = db['useraction']



    这里是通过字典的方式訪问数据库和集合,同一时候你也能够通过.(点号)的方式訪问


    做实验时。发现Python用Connection()连接时。改动操作速度非常快。而用MongoClient()建立的连接,操作速度慢非常多。


    from pymongo import MongoClient,Connection  
    import time 
    db = Connection('192.168.1.101', 27017).performance_test 
    #client = MongoClient('192.168.1.101', 27017)
    #db = client.performance_test 
    
    
    db.drop_collection("updates") 
    collection = db.updates 
    collection.insert({"x": 1}) 
    collection.find_one() 
    start = time.time() 
    for i in range(100000): 
            collection.update({}, {"$push" : {"x" : 1}}) 
    ...
    
    
    
    
    >python test_mongo_conn.py
    8.43799996376
    >python test_mongo_client.py
    62.5780000687






    用Connection() 8秒,MongoClient()则花了62秒。这性能相差也太多了。
    非常是疑惑,于是查了pymongo的文档,原来两者有个选项的默认行为不同:




    class pymongo.connection.Connection([host='localhost'[,port=27017[,max_pool_size=10[,network_timeout=None[, document_class=dict[, tz_aware=False[, **kwargs]]]]]]])


    Write Concern options:


    safe: Connection disables acknowledgement of write operations. Use safe=True to enable write acknowledgement.


    w: (integer or string) If this is a replica set, write operations will block until they have been replicated to the specified number or tagged set of servers. w=<int> always includes the replica set primary (e.g. w=3 means write to the primary and wait until replicated to two secondaries). Implies safe=True.


    wtimeout: (integer) Used in conjunction with w. Specify a value in milliseconds to control how long to wait for write propagation to complete. If replication does not complete in the given timeframe, a timeout exception is raised. Implies safe=True.


    j: If True block until write operations have been committed to the journal. Ignored if the server is running without journaling. Implies safe=True.


    fsync: If True force the database to fsync all files before returning. When used with j the server awaits the next group commit before returning. Implies safe=True.




    safe选项决定操作是“瞬时完毕”与“安全操作”。connection()默认是safe=False,即瞬时完毕,不等server回应,而MongoClient()默认是safe=True。即安全操作,等server确认后才继续下一步操作。
    所以一个8秒,一个62秒,这个差距实际上是“瞬时完毕”与“安全操作”两者的性能区别。


    当将Connection() 和MongoClient()建立连接时指定同样的safe參数,两者的性能表现是一样的。




    client = MongoClient('192.168.1.101', 27017,safe=False)
    #db = Connection('192.168.1.101', 27017,safe=False).performance_test

     

  • 相关阅读:
    解决 Mac launchpad 启动台 Gitter 图标无法删除的问题
    React 与 React-Native 使用同一个 meteor 后台
    解决 React-Native mac 运行报错 error Failed to build iOS project. We ran "xcodebuild" command but it exited with error code 65. To debug build logs further, consider building your app with Xcode.app, by ope
    一行命令更新所有 npm 依赖包
    swift学习笔记
    IOS语言总结
    focusSNS学习笔记
    别小看锤子,老罗真的很认真
    windowsphone开发页面跳转到另一个dll中的页面
    【令人振奋】【转】微软潘正磊谈DevOps、Visual Studio 2013新功能、.NET未来
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/7027765.html
Copyright © 2011-2022 走看看