本文整理匯總了Python中pool.Pool.borrowObject方法的典型用法代碼示例。如果您正苦於以下問題:Python Pool.borrowObject方法的具體用法?Python Pool.borrowObject怎麽用?Python Pool.borrowObject使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pool.Pool
的用法示例。
在下文中一共展示了Pool.borrowObject方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: TestPoolFunctions
# 需要導入模塊: from pool import Pool [as 別名]
# 或者: from pool.Pool import borrowObject [as 別名]
class TestPoolFunctions(unittest.TestCase):
def setUp(self):
self.pool = Pool(MsgPackPoolableFactory(), **config)
def tearDown(self):
unittest.TestCase.tearDown(self)
self.pool.destroy()
def test_ping(self):
try:
client = self.pool.borrowObject()
self.assertIsNotNone(client, "client is None")
rs = client.call("ping")
self.assertEqual(rs, "pong", "is pinged")
print rs
finally:
self.pool.returnObject(client)
示例2: Transaction
# 需要導入模塊: from pool import Pool [as 別名]
# 或者: from pool.Pool import borrowObject [as 別名]
class Transaction():
"""
基本用法1:
ta = Transaction(dbconfig, poolConfig)
@ta
def test(param1,param2,db):
return db.get("select now() as time")
test(param1,param2)
注意db參數在函數的 第一個
基本用法1:
ta=Transaction(dbconfig, poolConfig)
def fun():
//do something
ta.execute(fun)
example1:
dbconfig = {
"host":"127.0.0.1:4000",
"user":"root",
"password":"",
"database":"test"
}
poolConfig = {}
ta = Transaction(dbconfig, poolConfig)
@ta
def test(db):
return db.get("select now() as time")
@ta
def insert(name, birthday, is_relative,db):
return db.insert("insert into person(name,birthday,is_relative) values(%s,%s,%s)", name, birthday, is_relative)
class Test3():
@staticmethod
@ta
def test(db):
return db.get("select now() as time")
print test()
from datetime import date
print insert("x1", date.today(), 1)
print Test3.test()
example2:
def test(db):
return db.get("select now() as time")
def test2(db):
return db.query("select now() as time")
class Test3():
@staticmethod
def test(db):
return db.get("select now() as time")
class Test4():
def test(self,db):
return db.get("select now() as time")
t=Transaction(dbconfig, poolConfig)
rs = t.execute(test)
print rs.time
rses = t.execute(test2)
print [str(rs.time) for rs in rses]
rs = t.execute(Test3.test)
print rs.time
rs = t.execute(Test4().test)
print rs.time
rs= t.execute(lambda db:db.get("select now() as time"))
print rs.time
t.destory()
"""
def __init__(self, dbConfig=None, poolConfig=None, pool=None):
'''可給定dbConfig 和poolConfig 或者pool'''
if pool is None:
self._pool = Pool(Torndb2PoolableFactory(**dbConfig), **poolConfig)
else:
self._pool = pool
def destory(self):
self._pool.destroy()
def __call__(self, func):
def execute(*args,**kwargs):
try:
db = self._pool.borrowObject()
a = []
a.append(db)
for arg in args:
a.append(arg)
#.........這裏部分代碼省略.........