gpt4 book ai didi

python - 使用 SqlAlchemy 获取 TimeoutError

转载 作者:行者123 更新时间:2023-11-28 17:49:45 25 4
gpt4 key购买 nike

更新:刚刚发现 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com