简体   繁体   English

cx_Oracle 模块游标在python中关闭

[英]cx_Oracle module cursor close in python

I'm using cx_Oracle module in python.我在 python 中使用 cx_Oracle 模块。 Do we need to close opened cursors explicitly?我们是否需要明确关闭打开的游标? What will happen when we miss to close the cursor after fetching data and closing only the connection object (con.close()) without issuing cursor.close()?当我们在获取数据并仅关闭连接对象(con.close())而没有发出 cursor.close() 后错过关闭游标时会发生什么?

Will there be any chance of memory leak in this situation?在这种情况下会不会有内存泄漏的可能?

If you use multiple cursor.如果使用多个游标。 cursor.close() will help you to release the resources you don't need anymore. cursor.close() 将帮助您释放不再需要的资源。 If you just use one cursor with one connection.如果您只使用一个游标和一个连接。 I think connection.close() is fine.我认为 connection.close() 很好。

According to the documentation of cx_Oracle , the cursor should be garbage-collected automatically, and there should be no risk of a leak .根据cx_Oracle 的文档,游标应该被自动垃圾收集,并且应该没有泄漏的风险

However, in my anecdotal experience, if I didn't close the cursor explicitly, the memory usage of the process would grow without bounds -- this might have something to do with my usage of a custom rowfactory , where I had to capture the cursor in a lambda (although, in theory, the GC should be able to handle this case as well).但是,根据我的轶事经验,如果我没有明确关闭游标,则进程的内存使用量将无限制地增长——这可能与我使用自定义rowfactory ,我必须在其中捕获游标在 lambda 中(尽管理论上,GC 也应该能够处理这种情况)。

Since the Cursor class implements the context manager pattern, you can safely and conveniently write:由于 Cursor 类实现了上下文管理器模式,您可以安全方便地编写:

with connection.cursor() as cursor:
    cursor.execute("...")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM