-
Notifications
You must be signed in to change notification settings - Fork 159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inner async for loop caused cursor already closed error #535
Comments
Hi @playpauseandstop thx! we are trying to solve the problem I wrote a test for the reproduce the behavior import pytest
import sqlalchemy as sa
from sqlalchemy.sql.ddl import CreateTable
meta = sa.MetaData()
tbl = sa.Table('sa_tbl4', meta,
sa.Column('id', sa.Integer, nullable=False, primary_key=True),
sa.Column('name', sa.String(255), nullable=False,
default='default test'),
sa.Column('is_active', sa.Boolean, default=True))
@pytest.fixture
def engine(make_engine, loop):
async def start():
engine = await make_engine()
async with engine.acquire() as conn:
await conn.execute('DROP TABLE IF EXISTS sa_tbl4')
await conn.execute(CreateTable(tbl))
await conn.execute(tbl.insert().values())
await conn.execute(tbl.insert().values())
await conn.execute(tbl.insert().values())
return engine
return loop.run_until_complete(start())
async def test_view(engine):
async with engine.acquire() as conn:
data = await fetch_data(conn)
print(data)
async def fetch_data_item(conn):
result = []
async for item in conn.execute(tbl.select()):
result.append(item)
return result
async def fetch_data(conn):
result = []
async for item in conn.execute(tbl.select()):
result.extend((item, await fetch_data_item(conn)))
return result I checked this behavior after this PR #452
before this commit test passes
@thehesiod maybe you have ideas? |
@playpauseandstop I thought about this issue.
In the issues #364, it was decided to hold one cursor for one connection. In the synchronous version of We cannot do this in the asynchronous version, since the so you need to allocate a new connection from free pool if you want to make requests at the time of the iteration. It may be worth raising an error when trying to open a new cursor in one connection. We will also try as soon as possible to describe this behavior in the documentation. |
yep, please read from #364 (comment). Issue here is that |
I think we can do something similar to the then code samples will look like this: async def foo(engine):
async with engine.acquire() as conn:
print(await (await conn.execute('select ... ')).fetchall())
async for row in await conn.execute('select ... '):
print(row)
print(await (await conn.execute('select ... ')).fetchall())
async with conn.execute('insert or update...'):
pass
async for row in await conn.execute('select ... '):
async with conn.execute('insert ... %s', row):
pass But it requires discussion. |
that's not a good idea, a |
One cursor per connection sounds reasonable. |
Dear all. Please see this pull request #548. |
After upgrade to
aiopg==0.16.0
next pseudo-code causedcursor already closed
InterfaceError
,The exception itself is:
The error happened when running
aiohttp.web
server, as well as in tests, run bypytest
. However downgrade toaiopg==0.15.0
fixes the issue. Any ideas on why it is happened and maybe you need more debug information?OS: macOS 10.14.3, Ubuntu 16.04.3
Python version: 3.7.2
aiopg version: 0.16.0
The text was updated successfully, but these errors were encountered: