本文整理汇总了Python中twisted.enterprise.adbapi.ConnectionPool.close方法的典型用法代码示例。如果您正苦于以下问题:Python ConnectionPool.close方法的具体用法?Python ConnectionPool.close怎么用?Python ConnectionPool.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.enterprise.adbapi.ConnectionPool
的用法示例。
在下文中一共展示了ConnectionPool.close方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ConnectionPool
# 需要导入模块: from twisted.enterprise.adbapi import ConnectionPool [as 别名]
# 或者: from twisted.enterprise.adbapi.ConnectionPool import close [as 别名]
class ConnectionPool(object):
"""
Wrapper for twisted.enterprise.adbapi.ConnectionPool to use with tornado.
"""
def __init__(self, *args, **kwargs):
self._pool = TxConnectionPool(*args, **kwargs)
def run_query(self, *args, **kwargs):
return self._defer_to_future(self._pool.runQuery(*args, **kwargs))
def run_operation(self, *args, **kwargs):
return self._defer_to_future(self._pool.runOperation(*args, **kwargs))
def run_interaction(self, *args, **kwargs):
return self._defer_to_future(self._pool.runInteraction(*args, **kwargs))
def close(self):
self._pool.close()
@staticmethod
def _defer_to_future(defer):
future = TracebackFuture()
defer.addCallbacks(
future.set_result,
lambda failure: future.set_exc_info(
(failure.type, failure.value, failure.tb)))
return future
示例2: test_startedClose
# 需要导入模块: from twisted.enterprise.adbapi import ConnectionPool [as 别名]
# 或者: from twisted.enterprise.adbapi.ConnectionPool import close [as 别名]
def test_startedClose(self):
"""
If L{ConnectionPool.close} is called after it has been started, but
not by its shutdown trigger, the shutdown trigger is cancelled.
"""
reactor = EventReactor(True)
pool = ConnectionPool('twisted.test.test_adbapi', cp_reactor=reactor)
# There should be a shutdown trigger waiting.
self.assertEquals(reactor.triggers, [('during', 'shutdown', pool.finalClose)])
pool.close()
# But not anymore.
self.assertFalse(reactor.triggers)
示例3: test_unstartedClose
# 需要导入模块: from twisted.enterprise.adbapi import ConnectionPool [as 别名]
# 或者: from twisted.enterprise.adbapi.ConnectionPool import close [as 别名]
def test_unstartedClose(self):
"""
If L{ConnectionPool.close} is called without L{ConnectionPool.start}
having been called, the pool's startup event is cancelled.
"""
reactor = EventReactor(False)
pool = ConnectionPool('twisted.test.test_adbapi', cp_reactor=reactor)
# There should be a startup trigger waiting.
self.assertEquals(reactor.triggers, [('after', 'startup', pool._start)])
pool.close()
# But not anymore.
self.assertFalse(reactor.triggers)
示例4: __init__
# 需要导入模块: from twisted.enterprise.adbapi import ConnectionPool [as 别名]
# 或者: from twisted.enterprise.adbapi.ConnectionPool import close [as 别名]
class MDatabase:
"""
Sqlite database for Marnatarlo
"""
def __init__(self, dbname):
self.dbname = dbname
try:
fh = open(dbname)
except IOError as e:
conn = sqlite3.connect(dbname)
curs = conn.cursor()
curs.execute("Create table users (name text unique, password text)")
curs.execute("Create table stats(name text, played INTEGER, won INTEGER, FOREIGN KEY(name) REFERENCES users(name))")
conn.commit()
curs.close()
self.__dbpool = ConnectionPool('sqlite3', self.dbname)
def shutdown(self):
"""
Shutdown function
It's a required task to shutdown the database connection pool:
garbage collector doesn't shutdown associated thread
"""
self.__dbpool.close()
def returnOk(self, o):
return True
def returnFailure(self, o):
return False
def returnResult(self, result):
return result
def _returnResult(self, deferred, count=None):
if count:
return self.__dbpool.fetchmany(count)
else:
return self.__dbpool.fetchall()
def execSql(self, sql, params={}):
"""
Exec an SQL command, return True or False
@type sql C{str}
@param sql SQL command
"""
def run(sql, params):
return self.__dbpool.runQuery(sql, params)
d = run(sql, params)
d.addCallback(self._returnResult)
d.addErrback(self.returnFailure)
d.addCallback(self.returnResult)
return d
def fetch(self, sql, params={}):
"""
Exec an SQL command, fetching the rows resulting
@type sql C{str}
@param sql SQL command
"""
def run(sql, params):
return self.__dbpool.runQuery(sql, params)
d = run(sql, params)
d.addCallback(self.returnResult)
d.addErrback(self.returnFailure)
return d
def get_stats(self, user):
query = "SELECT * FROM stats WHERE name=?"
return self.fetch(query, (user,))
def user_won(self, user):
query = "UPDATE stats SET won=won+1 WHERE name=?"
return self.execSql(query, (user,))
def user_play(self, user):
query = "UPDATE stats SET played=played+1 WHERE name=?"
return self.execSql(query, (user,))
def save_user(self, user, passwd):
"""
Save user / password into db
@type user C{str}
@type password C{str}
"""
def insert_user(users, user, passwd):
if len(users) > 0:
return self.returnFailure(users)
query = "INSERT INTO users(name, password) VALUES (?, ?)"
self.execSql(query, (user, passwd,))
query = "INSERT INTO stats(name, played, won) VALUES (?, 0,0)"
return self.execSql(query, (user,))
return self.get_user_login_info(user).addCallback(insert_user, user, passwd)
def get_user_login_info(self, user):
"""
#.........这里部分代码省略.........
示例5: AbstractADBAPIDatabase
# 需要导入模块: from twisted.enterprise.adbapi import ConnectionPool [as 别名]
# 或者: from twisted.enterprise.adbapi.ConnectionPool import close [as 别名]
class AbstractADBAPIDatabase(object):
"""
A generic SQL database.
"""
def __init__(self, dbID, dbapiName, dbapiArgs, persistent, **kwargs):
"""
@param persistent: C{True} if the data in the DB must be perserved during upgrades,
C{False} if the DB data can be re-created from an external source.
@type persistent: bool
"""
self.dbID = dbID
self.dbapiName = dbapiName
self.dbapiArgs = dbapiArgs
self.dbapikwargs = kwargs
self.persistent = persistent
self.initialized = False
def __repr__(self):
return "<%s %r>" % (self.__class__.__name__, self.pool)
@inlineCallbacks
def open(self):
"""
Access the underlying database.
@return: a db2 connection object for this index's underlying data store.
"""
if not self.initialized:
self.pool = ConnectionPool(self.dbapiName, *self.dbapiArgs, **self.dbapikwargs)
# sqlite3 is not thread safe which means we have to close the sqlite3 connections in the same thread that
# opened them. We need a special thread pool class that has a thread worker function that does a close
# when a thread is closed.
if self.dbapiName == "sqlite3":
self.pool.threadpool.stop()
self.pool.threadpool = ConnectionClosingThreadPool(1, 1)
self.pool.threadpool.start()
self.pool.threadpool.pool = self.pool
#
# Set up the schema
#
# Create CALDAV table if needed
test = (yield self._test_schema_table())
if test:
version = (yield self._db_value_for_sql("select VALUE from CALDAV where KEY = 'SCHEMA_VERSION'"))
dbtype = (yield self._db_value_for_sql("select VALUE from CALDAV where KEY = 'TYPE'"))
if (version != self._db_version()) or (dbtype != self._db_type()):
if dbtype != self._db_type():
log.err("Database %s has different type (%s vs. %s)"
% (self.dbID, dbtype, self._db_type()))
# Delete this index and start over
yield self._db_remove()
yield self._db_init()
elif version != self._db_version():
log.err("Database %s has different schema (v.%s vs. v.%s)"
% (self.dbID, version, self._db_version()))
# Upgrade the DB
yield self._db_upgrade(version)
else:
yield self._db_init()
self.initialized = True
def close(self):
if self.initialized:
self.pool.close()
self.pool = None
self.initialized = False
@inlineCallbacks
def clean(self):
if not self.initialized:
yield self.open()
yield self._db_empty_data_tables()
@inlineCallbacks
def execute(self, sql, *query_params):
if not self.initialized:
yield self.open()
yield self._db_execute(sql, *query_params)
@inlineCallbacks
def executescript(self, script):
#.........这里部分代码省略.........
示例6: AbstractADBAPIDatabase
# 需要导入模块: from twisted.enterprise.adbapi import ConnectionPool [as 别名]
# 或者: from twisted.enterprise.adbapi.ConnectionPool import close [as 别名]
class AbstractADBAPIDatabase(object):
"""
A generic SQL database.
"""
def __init__(self, dbID, dbapiName, dbapiArgs, persistent, **kwargs):
"""
@param persistent: C{True} if the data in the DB must be perserved during upgrades,
C{False} if the DB data can be re-created from an external source.
@type persistent: bool
"""
self.dbID = dbID
self.dbapiName = dbapiName
self.dbapiArgs = dbapiArgs
self.dbapikwargs = kwargs
self.persistent = persistent
self.initialized = False
def __repr__(self):
return "<%s %r>" % (self.__class__.__name__, self.pool)
@inlineCallbacks
def open(self):
"""
Access the underlying database.
@return: a db2 connection object for this index's underlying data store.
"""
if not self.initialized:
self.pool = ConnectionPool(self.dbapiName, *self.dbapiArgs, **self.dbapikwargs)
# sqlite3 is not thread safe which means we have to close the sqlite3 connections in the same thread that
# opened them. We need a special thread pool class that has a thread worker function that does a close
# when a thread is closed.
if self.dbapiName == "sqlite3":
self.pool.threadpool.stop()
self.pool.threadpool = ConnectionClosingThreadPool(1, 1)
self.pool.threadpool.start()
self.pool.threadpool.pool = self.pool
#
# Set up the schema
#
# Create CALDAV table if needed
try:
test = (yield self._test_schema_table())
if test:
version = (yield self._db_value_for_sql("select VALUE from CALDAV where KEY = 'SCHEMA_VERSION'"))
dbtype = (yield self._db_value_for_sql("select VALUE from CALDAV where KEY = 'TYPE'"))
if (version != self._db_version()) or (dbtype != self._db_type()):
if dbtype != self._db_type():
log.error("Database %s has different type (%s vs. %s)"
% (self.dbID, dbtype, self._db_type()))
# Delete this index and start over
yield self._db_remove()
yield self._db_init()
elif version != self._db_version():
log.error("Database %s has different schema (v.%s vs. v.%s)"
% (self.dbID, version, self._db_version()))
# Upgrade the DB
yield self._db_upgrade(version)
else:
yield self._db_init()
self.initialized = True
except:
# Clean up upon error so we don't end up leaking threads
self.pool.close()
self.pool = None
raise
def close(self):
if self.initialized:
try:
self.pool.close()
except Exception, e:
log.error("Error whilst closing connection pool: %s" % (e,))
self.pool = None
self.initialized = False
示例7: SQLMagicPipeline
# 需要导入模块: from twisted.enterprise.adbapi import ConnectionPool [as 别名]
# 或者: from twisted.enterprise.adbapi.ConnectionPool import close [as 别名]
#.........这里部分代码省略.........
port = self.settings.get('port', 3050),
#dialect = 1, # necessary for all dialect 1 databases
charset = 'UTF8',# specify a character set for the connection
)
self.paramstyle = '?'
self.queries.update({
'insert': "INSERT INTO $table:esc ($fields) VALUES ($values)",
'update': "UPDATE $table:esc SET $fields_values WHERE $indices:and",
})
self.queries.update(kwargs.get('queries', {}))
@classmethod
def from_crawler(cls, crawler):
if not crawler.settings.get('SQLMAGIC_DATABASE'):
raise NotConfigured('No database connection settings found.')
o = cls(
settings=crawler.settings.get('SQLMAGIC_DATABASE'),
stats=crawler.stats,
queries=crawler.settings.get('SQLMAGIC_QUERIES', {}),
debug=crawler.settings.getbool('SQLMAGIC_DEBUG')
)
return o
def open_spider(self, spider):
self.on_connect()
def on_connect(self):
## override this to run some queries after connecting
# e.g. create tables for an in-memory SQLite database
pass
def close_spider(self, spider):
self.shutdown()
def shutdown(self):
"""Shutdown connection pool, kill associated threads"""
self.__dbpool.close()
def process_item(self, item, spider):
"""Process the item."""
# Only handle items inheriting SQLItem
if not isinstance(item, SQLItem):
return item
self.stats.inc_value('sqlmagic/total_items_caught')
# always return original item
deferred = self.operation(item, spider)
deferred.addBoth(lambda _: item)
return deferred
def operation(self, item, spider):
def on_insert(result, query, params):
self.stats.inc_value('sqlmagic/sqlop_success_insert')
if self.debug:
qlog = self._log_preparedsql(query, params)
log.msg('%s executed: %s' % (self.__class__.__name__, qlog), level=log.DEBUG, spider=spider)
return result
def on_update(result, query, params):
self.stats.inc_value('sqlmagic/sqlop_success_update')
if self.debug: