def late_init(self, db: Gino, *, loop=None, options=_options):
"""
Initialize this application with a database object.
This method does a few things to setup application for working with
the database:
- it enables task local storage;
- creates a connection pool and binds it to the passed database object;
- populates :py:attr:`~.db`.
:param db: the :py:class:`gino.ext.tornado.Gino()` class instance that
will be used in this application.
:param loop: io loop that will be used to run heep server, either
tornado's or asyncio's.
:param options: a tornado's ``OptionParser()`` instance or any
dictionary-like object with the database settings. Default is to
use ``tornado.options.options`` global.
"""
if loop is None:
loop = tornado.ioloop.IOLoop.current()
if isinstance(loop, tornado.platform.asyncio.BaseAsyncIOLoop):
asyncio_loop = loop.asyncio_loop
elif isinstance(loop, asyncio.BaseEventLoop):
asyncio_loop = loop
else:
raise RuntimeError('AsyncIOLoop is required to run GINO')
_enable_task_local(asyncio_loop)
self.db: Gino = db
await db.create_pool(
host=options['db_host'],
port=options['db_port'],
user=options['db_user'],
password=options['db_password'],
database=options['db_database'],
min_size=options['db_pool_min_size'],
max_size=options['db_pool_max_size'],
max_inactive_connection_lifetime=(
options['db_pool_max_inactive_conn_lifetime']
),
max_queries=options['db_pool_max_queries'],
loop=asyncio_loop
)
# noinspection PyAbstractClass
评论列表
文章目录