忽略/重新连接池中的已关闭连接

时间:2016-05-25 05:30:42

标签: postgresql python-3.x python-asyncio

我在pool.acquire()和池中关闭连接时遇到了一些问题。 在PG Server上设置连接超时为120秒。当我使用pool.acquire()时,因为连接已关闭而引发错误:

Sleep 150
Traceback (most recent call last):
  File "test.py", line 21, in <module>
    loop.run_until_complete(test_select())
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/base_events.py", line 337, in run_until_complete
    return future.result()
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/futures.py", line 274, in result
    raise self._exception
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/tasks.py", line 239, in _step
    result = coro.send(None)
  File "test.py", line 10, in test_select
async with pool.acquire() as conn:
  File "/usr/local/lib/python3.5/site-packages/aiopg/utils.py", line 116, in __aenter__
    self._conn = yield from self._coro
  File "/usr/local/lib/python3.5/site-packages/aiopg/pool.py", line 170, in _acquire
    assert not conn.closed, conn
AssertionError: <aiopg.connection.Connection object at 0x106f7df98>

和代码

import asyncio
import aiopg

dsn = 'dbname=dbname user=user password=password host=127.0.0.1'
async def test_select():
    async with aiopg.create_pool(dsn) as pool:
        print("Sleep 150")
        await asyncio.sleep(150)
        async with pool.acquire() as conn:
            async with conn.cursor() as cur:
                await cur.execute("SELECT 1")
                ret = []
                async for row in cur:
                    ret.append(row)
                assert ret == [(1,)]
    print("ALL DONE")


loop = asyncio.get_event_loop()
loop.run_until_complete(test_select())

如何解决这个问题。 谢谢!

0 个答案:

没有答案