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 ...
随机推荐
- 字符串及其操作,字符的Unicode编码
plainText=input('message:') for c in plainText: print(chr(ord(c)-3),end='') plainText=input('message ...
- el和jstl标签库讲解视频
https://www.bilibili.com/video/av22415283/?p=1
- clusterware启动顺序——OHASD
Clusterware启动顺序 [root@ebsdb1 etc]# crsctl check crs CRS-4638: Oracle High Availability Services is ...
- Python PIL
Python PIL PIL (Python Image Library) 库是Python 语言的一个第三方库,PIL库支持图像存储.显示和处理,能够处理几乎所有格式的图片. 一.PIL库简介 1. ...
- Spring4中使用通用Mapper
1. 在Spring4中使用通用Mapper Spring4增加了对泛型注入的支持,这个特性对通用Mapper来说,非常的有用,可以说有了这个特性,可以直接在Service中写Mapper<U ...
- Python01(linux基础)
聊一聊计算机 CPU,电源相关,时钟部分(主频),存储器,输入输出系统 计算机记忆装置:存储器分两大类:1.内存Ram(速度快,容量小(相对),断电后内容丢失) 非持久性记忆存储器2.如:硬盘,(速度 ...
- poi读取excel工具类
package com.manage.utils; import ch.qos.logback.core.net.SyslogOutputStream; import com.google.gson. ...
- 周强 201771010141《面对对象程序设计(java)》第十周学习总结
---恢复内容开始--- 1.实验目的与要求 (1) 理解泛型概念: (2) 掌握泛型类的定义与使用: (3) 掌握泛型方法的声明与使用: (4) 掌握泛型接口的定义与实现: (5)了解泛型程序设计, ...
- C#引用C++的DLL方案(C#调用非托管动态链接库)
SocketClientInit是C++里面定义的方法,通过EntryPoint = "?SocketClientInit@@YAHHHPAD@Z"指出这个函数的真正入口处,方法是 ...
- Day8 linux软件包管理
软件包的两种形式 qq.tar.gz (需要编译 源码翻译成二进制)/ rpm (直接安装) rpm的文件名分为5部分 name名称 version版本编号 release发布次数 ...