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 ...
随机推荐
- Asp .Net Core Spa (二) - 服务器渲染1
Server Side Rendering 服务器渲染是各 Spa 项目目前很热衷于解决的一个问题,毕竟针对SEO和首次加载优化 .Net Core SPA 服务器渲染 将分为 两篇: 第一篇 主要分 ...
- 5、AngularJS 直接绑定显示html ($sce、$sanitize服务)
1.直接使用$sce服务(angularjs中:$sce.trustAsHtml($scope.snippet).html:ng-bind-html="snippet") 以下代码 ...
- JavaScript 实时 全角转半角
//JavaScript全角字符转半角(参数str为input框输入的内容)var $fullChar2halfChar = function(str) { var result = ''; for ...
- 关于position的一些问题
position属性: static:静止 relative:相对的 fixed:固定的 absolu:绝对的 position的一些实例子如下: HTML: <!DOCTYPE htm ...
- centos 6.5 下 nginx 简单优化_虚拟主机_负载均衡
# 用了nginx for win很久,安装也是超级简单.# 还是用一下linux版的吧.环境是centos 6.5 x64 # 安装开始: # 先安装依赖 yum install gcc-c++ y ...
- CNN
<卷积神经网络详述> <卷积神经网络——雨石博客> 学习参考:http://blog.csdn.net/stdcoutzyx/article/details/4159 ...
- VBA中查找并选定文字
如果从 Selection 对象访问 Find 对象,当找到搜索条件时,就会更改所选内容.下列示例选定下一个出现的“Hello”.如果到达文档结尾时仍未找到“Hello”,则停止搜索. With Se ...
- windows与sql身份登录
windows身份验证:1.“.”代表本地2.127.0.0.1代表本地3.本地ip地址,在dos中可查4.localhost代表本地 sql身份验证:首先用windows登上去,之后再安全性中找到登 ...
- Python 进程池的回调函数
import os from multiprocessing import Pool,Process def f1(n): print('进程池里面的进程id',os.getpid()) print( ...
- Python使用requests库,requests.post格式会引发参数不存在问题
Content-Type: application/json #############↓↓↓成功↓↓↓############# import requests url = 'http://loca ...