在git找了几个blog的源码,在学习的过程中,发现有人使用Connection(),有人却在使用MongoClient(),那么到底两者有什么差别呢?

且看分析如下:

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,即瞬时完成,不等服务器回应,而MongoClient()默认是safe=True,即安全操作,等服务器确认后才继续下一步操作。

所以一个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

结论:Python用Connection()连接时,修改操作速度非常快,而用MongoClient()建立的连接,操作速度慢很多。

顺便分享账号密码登录的代码:


from pymongo import MongoClient
client = MongoClient('www.yeayee.com','')
client.database.authenticate("user","password")
db = client.database
collection = db.collection

最新文章

  1. MVVM大比拼之vue.js源码精析
  2. b/s 猫大叔
  3. struts 初体验
  4. hdu ----3695 Computer Virus on Planet Pandora (ac自动机)
  5. [原]Unity3d中奇怪的编译错误
  6. Python学习笔记1(基础语法)
  7. 用Java实现 ,冒泡排序与普通排序的区别
  8. CentOS配置本地yum源
  9. 疑难杂症:Java中Scanner连续获取int和String型发生错误.
  10. 用贝叶斯定理解决三门问题并用Python进行模拟(Bayes&#39; Rule Monty Hall Problem Simulation Python)
  11. Confluence 6 使用 Fail2Ban 来限制登录尝试
  12. [luogu 5300][bzoj 5502] [GXOI/GZOI2019] 与或和
  13. 改变自己从学习linux开始
  14. python 网络基础 之 网络协议
  15. [arm学习]makefile学习总结
  16. 树莓派进阶之路 (012) - 树莓派配置文档 config.txt 说明
  17. asp.net Hessian 服务的注册
  18. Top 25 Most Frequently Asked Interview Core Java Interview Questions And Answers
  19. 补交课下测试(ch12并发编程) 08.第八周
  20. CentOS6.6 编译Redis报错:&quot;Newer version of jemalloc required&quot;

热门文章

  1. Unpacking Argument Lists
  2. How To Use Google Flags
  3. 27.Remove Element(Array)
  4. tomcat限制ip访问
  5. 解题报告 - 577. Employee Bonus
  6. log4j 使用记录
  7. php利用mpdf生成pdf并保存到目录
  8. ARKit对安卓的提示 ARKit与Google Tango
  9. windows下 git+tortoiseGit的使用【转】
  10. Git config 配置文件