import dbapi2
conn = dbapi2.connect("db", autocommit=True)
def fetch_generator():
cursor = conn.cursor()
for res in cursor.execute('select * from table'):
yield res
def generator_1():
for x in fetch_generator():
yield x
def generator_2():
for x in generator_1():
yield x
if __name__ == '__main__':
for x in generator_2():
print(x)
This throws an error at for x in fetch_generator()
Error: dbapi2.InterfaceError: Attempted to use a closed cursor
Why is the cursor closing with the chain of generators? Is is a scope issue and are context managers one solution? How do I keep the cursor from having del
or close
called on it automatically -- somewhere not sure when -- and keep it open.