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,即瞬时完成,不等服务器回应,而MongoClient()默认是safe=True,即安全操作,等服务器确认后才继续下一步操作。
所以一个8秒,一个62秒,这个差距实际上是“瞬时完成”与“安全操作”两者的性能差别。
当将Connection() 和MongoClient()建立连接时指定相同的safe参数,两者的性能表现是一样的。
client = MongoClient('192.168.1.101', 27017,safe=False)
---------------------
作者:djd已经存在
来源:CSDN
原文:https://blog.csdn.net/djd1234567/article/details/47859015
版权声明:本文为博主原创文章,转载请附上博文链接!
pymongo中的连接操作:Connection()与MongoClient()的更多相关文章
- spark中各种连接操作以及有用方法
val a = sc.parallelize(Array(("123",4.0),("456",9.0),("789",9.0)) val ...
- 一起学Hadoop——实现两张表之间的连接操作
---恢复内容开始--- 之前我们都是学习使用MapReduce处理一张表的数据(一个文件可视为一张表,hive和关系型数据库Mysql.Oracle等都是将数据存储在文件中).但是我们经常会遇到处理 ...
- Python Pymongo中Connection()与MongoClient()差异
在git找了几个blog的源码,在学习的过程中,发现有人使用Connection(),有人却在使用MongoClient(),那么到底两者有什么差别呢? 且看分析如下: db = Connection ...
- JAVA中通过Jedis操作Redis连接与插入简单库
一.简述 JAVA中通过Jedis操作Redis连接与插入简单库 二.依赖 <!-- https://mvnrepository.com/artifact/redis.clients/jedis ...
- php大力力 [024节]PHP中的字符串连接操作(2015-08-27)
2015-08-27 php大力力024.PHP中的字符串连接操作 PHP中的字符串连接操作 阅读:次 时间:2012-03-25 PHP字符串的连接的简单实例 时间:2013-12-30 很多 ...
- python 连接操作 各类数据库
转载自MySQL Loners 一,python 操作 MySQL:详情见:这里 #!/bin/env python # -*- encoding: utf-8 -*- #-------------- ...
- mongoDB中的连接池(转载)
一.mongoDB中的连接池 刚上手MongoDB,在做应用时,受以前使用关系型数据库的影响,会考虑数据库连接池的问题! 关系型数据库中,我们做连接池无非就是事先建立好N个连接(connection) ...
- 连接字符串中Min Pool Size的理解是错误,超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。
Min Pool Size的理解是错误的 假设我们在一个ASP.NET应用程序的连接字符串中将Min Pool Size设置为30: <add name="cnblogs" ...
- 在Tomcat中配置连接池和数据源
1.DataSource接口介绍 (1)DataSource 概述 JDBC1.0原来是用DriverManager类来产生一个对数据源的连接.JDBC2.0用一种替代的方法,使用DataSource ...
随机推荐
- LoadLibrary 失败的解决
工作中遇到调用Loadlibrary 偶发失败的问题,不是必现,而且这种错误只是在程序初始化的时候出现,初始化成功后当然不会调用,而初始化也不是经常做的动作,所以查找原因起来比较麻烦,调试过程中发现有 ...
- centos 设置中文
[root@mts /]# vim /etc/sysconfig/i18n 1 LANG="en_US.UTF-8" 2 SYSFONT="latarcyrheb-sun ...
- 连接MySQL常用工具
database.properties 如下:url中coursesystem为将要连接的数据库名:username为该数据库设置权限时的用户名:如果设置了密码,再添一项password=你的密码 d ...
- 文件6. 查找替换.txt文本文件中的内容
servlet实现对文本文件的查找替换 .jsp界面 <form> <table> <tr> <td>选择文本文件:</td> <td ...
- html5滑动事件代码
$(".header").on("touchstart", function(e) { // 判断默认行为是否可以被禁用 if (e.cancelable) { ...
- 最近学习了Sqlite3数据库,写一下操作应用以及命令
首先使用Flask-SQLAlchemy管理数据库 使用pip安装:pip install flask-sqlalchemy 接着要配置数据库,定义模型 关于数据库的操作就不再写了.... 使用Fla ...
- DAY4:简单购物系统
根据前几天对于循环和列表的学习,做了一个简单的购物系统: 密码模块就是前篇文章已经做过就,直接调用过来就行,简单说一下该购物系统功能 1,展示货物,需要手动添加,haha 2,判断余额是否充足并充值 ...
- python实现批量压缩文件夹
前段时间碰到一个需要把目录下文件夹压缩的项目,但是度娘里没找到,只好自己写脚本了. #coding:utf-8 import os filePath = raw_input("请输入路径:& ...
- TCPIP学习笔记
TCP的连接 状态变化 各个状态的意义如下: LISTEN - 侦听来自远方TCP端口的连接请求: SYN-SENT -在发送连接请求后等待匹配的连接请求: SYN-RECEIVED - 在收到和发送 ...
- myls
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unist ...