python - 使用 SqlAlchemy 获取 TimeoutError

标签 python sqlalchemy nosetests

更新:刚刚发现 echo_pool=True 只显示主要事件,而是使用 echo_pool="debug"。

我必须明确关闭引擎连接,否则在使用 nosetest 和 sqlalchemy 时会出现 TimeoutError: QueuePool limit of size 5 overflow 10 reached, connection timed out, timeout 30 错误。

我正在关注有关如何 join a Session into an external transaction 的 SqlAlchemy 文档.唯一的区别是我使用的是范围 session 。这是有问题的代码:

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()

这是 echo_pool 调试输出:

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

谢谢!

最佳答案

我在 sqlalchemy 的组上问了同样的问题。

所以是的,根据 Michael Bayer 的说法,您确实需要明确关闭连接:

When you run the tests with unittest, unittest creates a new instance of your test class, which means all those test class instances will have self.connection present there and not returned to the pool.

https://groups.google.com/forum/?fromgroups=#!topic/sqlalchemy/eiQTXKn5ai8

关于python - 使用 SqlAlchemy 获取 TimeoutError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12367576/

相关文章:

python - 如何在 Django 中按模型过滤模型

Python openpyxl 注释形状属性

postgresql - sqlalchemy.exc.编程错误 : (ProgrammingError) there is no unique constraint matching given keys for referenced table

python - 是否可以在 noses setup.cfg 中设置环境变量

python - 为 pyspark 运行 nosetests

nosetests - 如何告诉 Nose 和覆盖范围不要在我的 virtualenv 中包含文件?

python - 升级到 django 5.0 "AttributeError: ' BlankChoiceIterator' 对象没有属性 '__len__' 后出现问题"

python - 如何在嵌套的 for 和 if 循环中重写以下邮政编码?

python - 在 SQLAlchemy 中使用正确的文件结构以及如何将数据添加到 db

python - 将 sqlalchemy 类序列化为 json