我正在 jupyter 笔记本上运行一些代码,有一些对 aws redshift 数据库的 SQL 查询。
问题是在执行这些查询后,即使我没有在笔记本上运行任何内容,看起来表仍处于读锁定状态。
当我关闭运行笔记本的终端时,锁定会释放。
运行代码示例
def met():
con=psycopg2.connect(host=
,user=
,password=
,port=
,dbname =)
table_data = pd.read_sql_query(query, con)
con.close()
最佳答案
您的光标可能保持打开状态,您应该将其包装在 try- except-finally 中,以确保它在完成查询后关闭:
cursor = None
try:
cursor = db.cursor()
cursor.execute("""SELECT foo FROM bar""")
module.rase_unexpected_error()
cursor.commit()
except BaseException:
if cursor is not None:
cursor.rollback()
finally:
if cursor is not None:
cursor.close()
查看此问题以获取更多信息:use try/except with psycopg2 or "with closing"?
关于python - Jupyter笔记本空闲锁定 Redshift 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54709940/