本文整理汇总了Python中pool.Pool.put方法的典型用法代码示例。如果您正苦于以下问题:Python Pool.put方法的具体用法?Python Pool.put怎么用?Python Pool.put使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pool.Pool
的用法示例。
在下文中一共展示了Pool.put方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: DB
# 需要导入模块: from pool import Pool [as 别名]
# 或者: from pool.Pool import put [as 别名]
class DB(object):
"manage pooled database access"
def __init__(self, connect_config=None):
"""set up connection pool if config is available.
This allows the legacy pattern of app setup
by calling init_db to continue.
"""
if connect_config:
self.init(connect_config)
def init(self, connect_config):
"set up connection pools"
# allow for legacy config
# - check for space and comma delimited strings
if type(connect_config) == str:
if ',' in connect_config:
delim = ','
else:
delim = ' '
connect_config = connect_config.split(delim)
print ('Deprecated: database connection should be a tuple not a string')
host, user, pw = connect_config
self.conn_pool = Pool(Constructor(self.connect
, host, user, pw
, charset='utf8'), poolsize=8)
self.async_pool = adbapi.ConnectionPool('MySQLdb'
, host, user, pw
, charset='utf8'
, cursorclass=DictCursor)
def execute(self, sql, args=None):
"perform a query safely"
dbc = self.conn_pool.get()
db = dbc.cursor(DictCursor)
try:
db.execute(sql, args)
except OperationalError:
dbc = self.conn_pool.get()
db = dbc.cursor(DictCursor)
db.execute(sql, args)
if 'INSERT' in sql.upper():
# return the insert id
res = dbc.insert_id()
else:
# return the result seT
res = db.fetchall()
db.close()
# Disconnect(dbc)
self.conn_pool.put(dbc)
del db, dbc
return res
@inlineCallbacks
def aexecute(self, sql, *params):
"execute query asyncrously"
if 'INSERT' in sql.upper():
# for INSERT we would like to return the insert id
def insert(db):
"insert a record, return its insert id"
db.execute(sql, params)
return db.connection.insert_id()
uid = yield self.async_pool.runInteraction(insert)
returnValue(uid)
else:
l = yield self.async_pool.runQuery(sql, *params)
returnValue(l)
def connect(self, *args, **kwargs):
"connect to MySQL and intialise connection. Used by syncrous interface"
dbc = MySQLconnect(*args, **kwargs)
# override this method to give the required answer at all times!
dbc.character_set_name = lambda: 'utf8'
return dbc