Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python Gino中获取池连接(异步)_Python_Postgresql_Asyncpg_Gino - Fatal编程技术网

在Python Gino中获取池连接(异步)

在Python Gino中获取池连接(异步),python,postgresql,asyncpg,gino,Python,Postgresql,Asyncpg,Gino,我正在使用Postgres、Python3.7和asyncio+asyncpg+gino ORM ish+aiohttp路由、web响应 我在数据库testdb中创建了一个小postgres表users,并插入了一行: testdb=# select * from users; id | nickname ----+---------- 1 | fantix 我正在尝试设置我的数据库,以便在收到请求时可以在路由中使用ORM 导入时间 导入异步 进口吉诺 DATABASE\u URL=os

我正在使用Postgres、Python3.7和asyncio+asyncpg+gino ORM ish+aiohttp路由、web响应

我在数据库testdb中创建了一个小postgres表users,并插入了一行:

testdb=# select * from users;
 id | nickname
----+----------
  1 | fantix
我正在尝试设置我的数据库,以便在收到请求时可以在路由中使用ORM

导入时间 导入异步 进口吉诺 DATABASE\u URL=os.environ.get'DATABASE\u URL' db=gino.gino 类Userdb.Model: __tablename_uu='users' id=db.Columndb.Integer,主键=True 昵称=db.Columndb.Unicode,默认值为 kwargs=dict 最小尺寸=10, 最大尺寸=100, 最大查询数=1000, 最大非活动连接寿命=60*5, 回声=真 异步def测试引擎隐式: 等待db.set_bindDATABASE_URL,**kwargs return wait User.query.gino。所有这些都有效 异步def测试引擎显式: engine=等待gino.create_engineDATABASE_URL,**kwargs db.bind=引擎 与engine.acquire作为conn异步: return wait conn.allUser.select此选项无效! users=asyncio.get\u event\u loop.run\u直到\u completetest\u engine\u隐式 printf'隐式查询:{users}' users=asyncio.get\u event\u loop.run\u直到\u completetest\u引擎\u显式运行 printf'显式查询:{users}' 输出为:

web_1    | INFO gino.engine._SAEngine SELECT users.id, users.nickname FROM users
web_1    | INFO gino.engine._SAEngine ()
web_1    | implicit query: [<db.User object at 0x7fc57be42410>]
web_1    | INFO gino.engine._SAEngine SELECT
web_1    | INFO gino.engine._SAEngine ()
web_1    | explicit query: [()]
这很奇怪。显式代码实质上是对数据库运行一个简单的SELECT,这是无用的

我在文档中找不到一种方法既可以使用ORM,也可以从池中显式签出连接

我的问题是:

是否等待User.query.gino.all从池中签出连接?它是如何发布的? 如何在事务中封装查询?我感到不安的是,我无法明确控制何时/何地从池中获取连接,以及如何释放连接。
我基本上希望test_engine_explicit中风格的明确性能够与Gino一起工作,但也许我只是不理解Gino ORM是如何工作的

我以前从未使用过GINO,但在快速查看代码后:

基诺连接简单。因此,如果您提供了bare User.select,那么它不会添加任何内容。 如果要实现与使用User.query.gino.all相同的功能,但要自己维护连接,则可以使用User.query而不是普通用户。选择: 与engine.acquire作为conn异步: return wait conn.allUser.query 刚刚测试过,对我来说很好

关于连接池,我不确定我是否正确回答了这个问题,但是Engine.acquire然后将其添加到池中,这实际上是一个堆栈:

GINO中也有一个,例如,您可以手动创建一个不可重用、不可重用的连接并控制事务流:

与引擎异步。收单机构EUSE=False,可重复使用=False作为连接: tx=等待连接事务 尝试: 等待conn.status插入usersnickname值“e” 等待发送 除例外情况外: 等待发送回滚 提升 至于连接释放,我找不到任何证据表明GINO释放了连接本身。我猜这个池是由SQLAlchemy核心维护的

我肯定没有直接回答你的问题,但希望它能对你有所帮助

   :param reusable: Mark this connection as reusable or otherwise. This
      has no effect if it is a reusing connection. All reusable connections
      are placed in a stack, any reusing acquire operation will always
      reuse the top (latest) reusable connection. One reusable connection
      may be reused by several reusing connections - they all share one
      same underlying connection. Acquiring a connection with
      ``reusable=False`` and ``reusing=False`` makes it a cleanly isolated
      connection which is only referenced once here.