您可以使用Context Manager,例如(包含一些伪代码):
from contextlib import contextmanager
@contextmanager
def func():
sql =" select some rows "
dbconn = "connect and open to dtabase code"
yield execute(sql) # returns this really a generator?
dbclose #pseudocode, you probably want to put this in a try/finally block
with func() as result:
for it in result:
do something with it
当然,这仅在execute(sql)
真正返回生成器时才有用。如果在关闭连接之前将所有数据放入列表(因此放入内存),您的问题将过时。
def func():
sql =" select some rows "
dbconn = "connect and open to dtabase code"
ret = list( execute(sql) )
dbclose # no problem here, since all data is already fetched
return ret
回应您的评论:
fetchmany
如果您的数据库适配器遵循 python DB API 规范,一种有效的方法是多次获取一堆行。
以下代码以 100 个块为单位获取行,并dbclose
在执行离开with
块时显式调用:
def cursor_iter(cursor, num_of_rows=100):
while True:
rows = cursor.fetchmany(num_of_rows)
if not rows: break
for row in rows:
yield row
@contextmanager
def func():
sql = "select some rows"
dbconn = connect_and_open_database()
cursor = dbconn.cursor()
cursor.execute(sql)
yield cursor_iter(cursor)
dbclose()
with func() as result:
for row in result:
do_something(row)