<tfoot id='ECpaR'></tfoot>
      1. <small id='ECpaR'></small><noframes id='ECpaR'>

        <i id='ECpaR'><tr id='ECpaR'><dt id='ECpaR'><q id='ECpaR'><span id='ECpaR'><b id='ECpaR'><form id='ECpaR'><ins id='ECpaR'></ins><ul id='ECpaR'></ul><sub id='ECpaR'></sub></form><legend id='ECpaR'></legend><bdo id='ECpaR'><pre id='ECpaR'><center id='ECpaR'></center></pre></bdo></b><th id='ECpaR'></th></span></q></dt></tr></i><div id='ECpaR'><tfoot id='ECpaR'></tfoot><dl id='ECpaR'><fieldset id='ECpaR'></fieldset></dl></div>
        <legend id='ECpaR'><style id='ECpaR'><dir id='ECpaR'><q id='ECpaR'></q></dir></style></legend>

        • <bdo id='ECpaR'></bdo><ul id='ECpaR'></ul>

        如何关闭 MySQL 中的 sqlalchemy 连接

        时间:2023-06-02
      2. <i id='j56Ms'><tr id='j56Ms'><dt id='j56Ms'><q id='j56Ms'><span id='j56Ms'><b id='j56Ms'><form id='j56Ms'><ins id='j56Ms'></ins><ul id='j56Ms'></ul><sub id='j56Ms'></sub></form><legend id='j56Ms'></legend><bdo id='j56Ms'><pre id='j56Ms'><center id='j56Ms'></center></pre></bdo></b><th id='j56Ms'></th></span></q></dt></tr></i><div id='j56Ms'><tfoot id='j56Ms'></tfoot><dl id='j56Ms'><fieldset id='j56Ms'></fieldset></dl></div>

          <tbody id='j56Ms'></tbody>

          <small id='j56Ms'></small><noframes id='j56Ms'>

              <bdo id='j56Ms'></bdo><ul id='j56Ms'></ul>
            • <tfoot id='j56Ms'></tfoot>

                <legend id='j56Ms'><style id='j56Ms'><dir id='j56Ms'><q id='j56Ms'></q></dir></style></legend>
                  本文介绍了如何关闭 MySQL 中的 sqlalchemy 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这是我想运行的示例代码:

                  This is a sample code I'd like to run:

                  for i in range(1,2000):
                      db = create_engine('mysql://root@localhost/test_database')
                      conn = db.connect()
                      #some simple data operations
                      conn.close()
                      db.dispose()
                  

                  有没有一种方法可以在不从 MySQL 中收到连接过多"错误的情况下运行它?我已经知道我可以以其他方式处理连接或拥有连接池.我只想了解如何正确关闭来自 sqlalchemy 的连接.提前致谢!

                  Is there a way of running this without getting "Too many connections" errors from MySQL? I already know I can handle the connection otherwise or have a connection pool. I'd just like to understand how to properly close a connection from sqlalchemy. Thanks in advance!

                  推荐答案

                  以下是正确编写代码的方法:

                  Here's how to write that code correctly:

                  db = create_engine('mysql://root@localhost/test_database')
                  for i in range(1,2000):
                      conn = db.connect()
                      #some simple data operations
                      conn.close()
                  db.dispose()
                  

                  也就是说,Engine 是连接的工厂以及连接的,而不是连接本身.当你说conn.close()时,连接被返回到引擎内的连接池,而不是实际关闭.

                  That is, the Engine is a factory for connections as well as a pool of connections, not the connection itself. When you say conn.close(), the connection is returned to the connection pool within the Engine, not actually closed.

                  如果您确实希望连接实际关闭,即不池化,请通过 NullPool 禁用池化:

                  If you do want the connection to be actually closed, that is, not pooled, disable pooling via NullPool:

                  from sqlalchemy.pool import NullPool
                  db = create_engine('mysql://root@localhost/test_database', poolclass=NullPool)
                  

                  使用上述Engine 配置,每次调用conn.close() 都会关闭底层的DBAPI 连接.

                  With the above Engine configuration, each call to conn.close() will close the underlying DBAPI connection.

                  如果 OTOH 你真的想在每次调用时连接到不同数据库,也就是说,你的硬编码 "localhost/test_database" 只是一个例子,你实际上有很多不同的数据库,那么使用 dispose() 的方法是好的;它将关闭所有未从池中检出的连接.

                  If OTOH you actually want to connect to different databases on each call, that is, your hardcoded "localhost/test_database" is just an example and you actually have lots of different databases, then the approach using dispose() is fine; it will close out every connection that is not checked out from the pool.

                  在上述所有情况下,重要的是Connection 对象是通过close() 关闭的.如果您使用任何类型的无连接"执行,即 engine.execute()statement.execute()ResultProxy从执行调用返回的对象应该被完全读取,或者通过 close() 显式关闭.仍然打开的 ConnectionResultProxy 将禁止 NullPooldispose() 方法关闭每个最后一个连接.

                  In all of the above cases, the important thing is that the Connection object is closed via close(). If you're using any kind of "connectionless" execution, that is engine.execute() or statement.execute(), the ResultProxy object returned from that execute call should be fully read, or otherwise explicitly closed via close(). A Connection or ResultProxy that's still open will prohibit the NullPool or dispose() approaches from closing every last connection.

                  这篇关于如何关闭 MySQL 中的 sqlalchemy 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:MySQL - 在 WHERE 子句中使用 COUNT(*) 下一篇:MySQL处于“正在发送数据"状态是什么意思?

                  相关文章

                    <tfoot id='ABP3E'></tfoot>

                    <legend id='ABP3E'><style id='ABP3E'><dir id='ABP3E'><q id='ABP3E'></q></dir></style></legend>
                    1. <i id='ABP3E'><tr id='ABP3E'><dt id='ABP3E'><q id='ABP3E'><span id='ABP3E'><b id='ABP3E'><form id='ABP3E'><ins id='ABP3E'></ins><ul id='ABP3E'></ul><sub id='ABP3E'></sub></form><legend id='ABP3E'></legend><bdo id='ABP3E'><pre id='ABP3E'><center id='ABP3E'></center></pre></bdo></b><th id='ABP3E'></th></span></q></dt></tr></i><div id='ABP3E'><tfoot id='ABP3E'></tfoot><dl id='ABP3E'><fieldset id='ABP3E'></fieldset></dl></div>

                      <small id='ABP3E'></small><noframes id='ABP3E'>

                        <bdo id='ABP3E'></bdo><ul id='ABP3E'></ul>