如何在Python中返回生成器

时间:2013-09-10 07:18:12

标签: python generator

我正在考虑设计我的函数以结合数据库查询返回生成器。但对迭代器的概念有一些疑问

def func():
    sql =" select some rows "
    dbconn = "connect and open to dtabase code"
    ret = ( execute(sql)  ) <----- returning a generator?
    dbclose <----  I close the db connection here, but it gives me error
    return ret

问题是,当我在main函数中迭代生成器时,我点击“关闭游标时出错”。我应该在func()中关闭还是不关闭它?我想当调用func()结束时,dbconn变量将超出范围,我不应该担心关闭?

 # main function
 for it in func():
     do something with it
 close dbconn here?

我该如何设计?像列表一样撤回数据结构会更好吗? 感谢

3 个答案:

答案 0 :(得分:2)

您可以使用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

回应你的评论:

如果您的数据库适配器遵循python DB API规范,那么有效的方法是多次获取fetchmany行的一堆行。

以下代码以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)

答案 1 :(得分:1)

我没有太多使用数据库的经验,但我认为您应该检索查询结果并将其作为列表返回。如果你真的需要一个迭代器(但我不知道为什么),那么在列表ret上返回一个迭代器:

def func():
    sql =" select some rows "
    dbconn = "connect and open to dtabase code"
    ret = execute(sql)              # a list
    dbclose()
    return (elmt for elmt in ret)   # return an iterator over ret 

现在,如果它们存在一种检索查询的 nth 元素的方法,那么execute(sql, n)如果None太大就会返回n之类的内容,然后你可以使用yield:

 def func():
    sql =" select some rows "
    dbconn = "connect and open to dtabase code"

    n = 0
    ret = execute(sql,n)    # return the n-th element
    while ret is not None:
        yield ret
        n += 1
        ret = execute(sql,n)

    dbclose()

现在,这不是我建议的,主要是因为在迭代器未完成时,与db的连接保持打开状态。如果某些事情失败或设计不当,它可能永远不会发生。

答案 2 :(得分:0)

关闭数据库连接后,无法尝试操作游标 我将尝试这种方法:

def func(params):
    sql = "query to execute"
    cursor = execute(sql, params)
    return cursor.fetchall() # retrieves all posible results as a sequence of sequences,
                             # i.g. list of tuples(*)

### Main ###
# Open database connection
# Create cursor
for elem in func(): # Call to retrieve desired element's method and do something with that
    # Do something
# Close cursor
# Close database connection

(*)http://www.python.org/dev/peps/pep-0249/

我希望它有所帮助