Python 使用SqlAlchemy获取TimeoutError

Python 使用SqlAlchemy获取TimeoutError,python,sqlalchemy,nosetests,Python,Sqlalchemy,Nosetests,更新:刚刚发现echo\u pool=True只显示主要事件,而是使用echo\u pool=“debug” 我必须显式关闭引擎连接,否则会出现TimeoutError:QueuePool大小限制5溢出10达到,连接超时,使用NoTest和sqlalchemy时超时30错误 我正在遵循SqlAlchemy文档,了解如何使用。唯一的区别是我使用的是scopedsession。以下是违规代码: import unittest from sqlalchemy import create_engine

更新:刚刚发现echo\u pool=True只显示主要事件,而是使用echo\u pool=“debug”

我必须显式关闭引擎连接,否则会出现
TimeoutError:QueuePool大小限制5溢出10达到,连接超时,使用NoTest和sqlalchemy时超时30
错误

我正在遵循SqlAlchemy文档,了解如何使用。唯一的区别是我使用的是scopedsession。以下是违规代码:

import unittest

from sqlalchemy import create_engine

from myapp.mymodel import Session

engine = create_engine(
    '<REDACTED>',
    echo = False,
    # To make it faster to fail, but also fails with the default options
    # pool_size=2,
    # max_overflow=0,
    # echo_pool="debug",
    # pool_timeout=10,
)


class MyTest(unittest.TestCase):

    def setUp(self):
        self.connection = engine.connect()

        # begin a non-ORM transaction
        self.trans = self.connection.begin()

        # bind an individual Session to the connection
        Session.configure(bind=self.connection)

        self.addCleanup(self._teardown)

    def _teardown(self):
        """Rollback the db.

        Added to the list of cleanup by setUp, so that subclass do not have to
        call super() on tearDown.
        """

        # Rollback database
        self.trans.rollback()

        # Session must be closed BEFORE being removed
        Session.close()
        Session.remove()
        # If I don't do that, I get TimeOut
        # self.connection.close()

谢谢

我对sqlalchemy的团队问了同样的问题

迈克尔·拜耳说,是的,你确实需要明确地关闭连接:

当您使用unittest运行测试时,unittest将创建测试类的新实例,这意味着所有这些测试类实例都将具有self.connection,而不会返回到池中

2012-09-11 12:34:28,506 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bae1e20>
2012-09-11 12:34:28,514 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bae1e20> checked out from pool
2012-09-11 12:34:29,664 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bc61c20>
2012-09-11 12:34:29,665 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bc61c20> checked out from pool
2012-09-11 12:34:30,368 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bc9ea20>
2012-09-11 12:34:30,369 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bc9ea20> checked out from pool
2012-09-11 12:34:31,042 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bcd6820>
2012-09-11 12:34:31,043 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bcd6820> checked out from pool
2012-09-11 12:34:31,775 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bcbd820>
2012-09-11 12:34:31,775 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bcbd820> checked out from pool
2012-09-11 12:34:32,439 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bcc2220>
2012-09-11 12:34:32,439 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bcc2220> checked out from pool
2012-09-11 12:34:33,129 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bd0e220>
2012-09-11 12:34:33,129 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bd0e220> checked out from pool
2012-09-11 12:34:33,802 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bd1e420>
2012-09-11 12:34:33,802 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bd1e420> checked out from pool
2012-09-11 12:34:34,590 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bcf6a20>
2012-09-11 12:34:34,590 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bcf6a20> checked out from pool
2012-09-11 12:34:35,452 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bd53420>
2012-09-11 12:34:35,452 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bd53420> checked out from pool
2012-09-11 12:34:36,276 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bd32420>
2012-09-11 12:34:36,276 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bd32420> checked out from pool
2012-09-11 12:34:36,970 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bd80420>
2012-09-11 12:34:36,971 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bd80420> checked out from pool
2012-09-11 12:34:37,639 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bda9020>
2012-09-11 12:34:37,640 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bda9020> checked out from pool
2012-09-11 12:34:37,664 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bdc3c20>
2012-09-11 12:34:37,664 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bdc3c20> checked out from pool
2012-09-11 12:34:37,675 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bd95e20>
2012-09-11 12:34:37,675 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bd95e20> checked out from pool