本文整理汇总了Python中pymongo.connection.Connection.paired方法的典型用法代码示例。如果您正苦于以下问题:Python Connection.paired方法的具体用法?Python Connection.paired怎么用?Python Connection.paired使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pymongo.connection.Connection
的用法示例。
在下文中一共展示了Connection.paired方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _connect
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import paired [as 别名]
def _connect(self):
self._conn = None
try:
if len(self.master_args) == 2:
self._conn = Connection.paired(
(str(self.master_args[0]['host']), int(self.master_args[0]['port'])),
(str(self.master_args[1]['host']), int(self.master_args[1]['port'])),
pool_size=int(self.master_args[0]['query'].get('pool_size','16')))
else:
if self.master_args:
try:
network_timeout = self.master_args[0]['query'].get('network_timeout')
if network_timeout is not None:
network_timeout = float(network_timeout)
master = Connection(str(self.master_args[0]['host']), int(self.master_args[0]['port']),
pool_size=int(self.master_args[0]['query'].get('pool_size','16')),
network_timeout=network_timeout)
###authenticating when the db requires it to be done.
if self.master_args[0].get('username') and self.master_args[0].get('password'):
db = database.Database(master, self.master_args[0]['path'][1:])
db.authenticate(self.master_args[0]['username'],
self.master_args[0]['password'])
###
except:
if self.slave_args:
log.exception('Cannot connect to master: %s will use slave: %s' % (self.master_args, self.slave_args))
# and continue... to use the slave only
master = None
else:
raise
else:
log.info('No master connection specified, using slaves only: %s' % self.slave_args)
master = None
if self.slave_args:
slave = []
for a in self.slave_args:
network_timeout = a['query'].get('network_timeout')
if network_timeout is not None:
network_timeout = float(network_timeout)
slave.append(
Connection(str(a['host']), int(a['port']),
pool_size=int(a['query'].get('pool_size','16')),
slave_okay=True,
network_timeout=network_timeout,
)
)
if master:
self._conn = MasterSlaveConnection(master, slave)
else:
self._conn = slave[0]
else:
self._conn = master
except:
log.exception('Cannot connect to %s %s' % (self.master_args, self.slave_args))
else:
#log.info('Connected to %s %s' % (self.master_args, self.slave_args))
pass
return self._conn
示例2: test_repr
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import paired [as 别名]
def test_repr(self):
self.skip()
connection = Connection.paired(self.left, self.right)
self.assertEqual(repr(connection),
"Connection.paired(('%s', %s), ('%s', %s))" %
(self.left[0],
self.left[1],
self.right[0],
self.right[1]))
示例3: test_end_request
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import paired [as 别名]
def test_end_request(self):
self.skip()
connection = Connection.paired(self.left, self.right)
db = connection.pymongo_test
for _ in range(100):
db.test.remove({})
db.test.insert({})
self.assert_(db.test.find_one())
connection.end_request()
示例4: test_basic
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import paired [as 别名]
def test_basic(self):
self.skip()
connection = Connection.paired(self.left, self.right)
db = connection.pymongo_test
db.drop_collection("test")
a = {"x": 1}
db.test.save(a)
self.assertEqual(a, db.test.find_one())
示例5: get_mongo_collection
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import paired [as 别名]
def get_mongo_collection():
"Open a connection to MongoDB and return the collection to use."
if settings.RIGHT_MONGODB_HOST:
connection = Connection.paired(
left=(settings.MONGODB_HOST, settings.MONGODB_PORT),
right=(settings.RIGHT_MONGODB_HOST, settings.RIGHT_MONGODB_PORT)
)
else:
connection = Connection(host=settings.MONGODB_HOST, port=settings.MONGODB_PORT)
return connection[settings.MONGODB_DB][settings.MONGODB_COLLECTION]
示例6: test_connect
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import paired [as 别名]
def test_connect(self):
self.skip()
self.assertRaises(ConnectionFailure, Connection.paired,
self.bad, self.bad)
connection = Connection.paired(self.left, self.right)
self.assert_(connection)
host = connection.host
port = connection.port
connection = Connection.paired(self.right, self.left)
self.assert_(connection)
self.assertEqual(host, connection.host)
self.assertEqual(port, connection.port)
slave = self.left == (host, port) and self.right or self.left
self.assertRaises(ConfigurationError, Connection.paired,
slave, self.bad)
self.assertRaises(ConfigurationError, Connection.paired,
self.bad, slave)
示例7: test_pooling
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import paired [as 别名]
def test_pooling(self):
self.skip()
connection = Connection.paired(self.left, self.right,
pool_size=3,
auto_start_request=False)
db = connection.pymongo_test
for _ in range(100):
connection.start_request()
db.test.remove({})
db.test.insert({})
self.assert_(db.test.find_one())
connection.end_request()
示例8: _connect
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import paired [as 别名]
def _connect(self):
self._conn = None
try:
if len(self.master_args) == 2:
self._conn = Connection.paired(
(str(self.master_args[0]['host']), int(self.master_args[0]['port'])),
(str(self.master_args[1]['host']), int(self.master_args[1]['port'])))
else:
if self.master_args:
try:
network_timeout = self.master_args[0]['query'].get('network_timeout')
if network_timeout is not None:
network_timeout = float(network_timeout)
master = Connection(str(self.master_args[0]['host']), int(self.master_args[0]['port']),
network_timeout=network_timeout)
except:
if self.slave_args:
log.exception('Cannot connect to master: %s will use slave: %s' % (self.master_args, self.slave_args))
# and continue... to use the slave only
master = None
else:
raise
else:
log.info('No master connection specified, using slaves only: %s' % self.slave_args)
master = None
if self.slave_args:
slave = []
for a in self.slave_args:
network_timeout = a['query'].get('network_timeout')
if network_timeout is not None:
network_timeout = float(network_timeout)
slave.append(
Connection(str(a['host']), int(a['port']),
slave_okay=True,
network_timeout=network_timeout,
)
)
if master:
self._conn = MasterSlaveConnection(master, slave)
else:
self._conn = slave[0]
else:
self._conn = master
except:
log.exception('Cannot connect to %s %s' % (self.master_args, self.slave_args))
else:
#log.info('Connected to %s %s' % (self.master_args, self.slave_args))
pass
return self._conn
示例9: test_paired_connections_pass_individual_connargs
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import paired [as 别名]
def test_paired_connections_pass_individual_connargs(self):
c = Connection.paired(self.left, self.right, network_timeout=5)
self.assertEqual(5, c._Connection__network_timeout)
示例10: Something
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import paired [as 别名]
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Simple script to help test auto-reconnection."""
import threading
import time
from pymongo.errors import ConnectionFailure
from pymongo.connection import Connection
db = Connection.paired(("localhost", 27018), pool_size=10).test
db.test.remove({})
class Something(threading.Thread):
def run(self):
while True:
time.sleep(1)
try:
id = db.test.save({"x": 1})
assert db.test.find_one(id)["x"] == 1
db.test.remove(id)
db.connection().end_request()
print "Y"
except ConnectionFailure, e:
print e
print "N"
示例11: Something
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import paired [as 别名]
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Simple script to help test auto-reconnection."""
import sys
import threading
import time
sys.path[0:0] = [""]
from pymongo.errors import AutoReconnect
from pymongo.connection import Connection
db = Connection.paired(("localhost", 27018)).test
db.test.remove({})
class Something(threading.Thread):
def run(self):
while True:
time.sleep(1)
try:
id = db.test.save({"x": 1}, safe=True)
assert db.test.find_one(id)["x"] == 1
db.test.remove(id)
db.connection.end_request()
print "Y"
except AutoReconnect, e:
print e
示例12: test_paired_connections_pass_individual_connargs
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import paired [as 别名]
def test_paired_connections_pass_individual_connargs(self):
c = Connection.paired(self.left, self.right, slave_okay=True)
self.assertTrue(c.__slave_okay)