首页 > 解决方案 > Jupyter notebook 空闲锁定红移表

问题描述

我在 jupyter notebook 上运行一些代码,有一些对 aws redshift 数据库的 SQL 查询。

问题是在执行这些查询之后,即使我没有在笔记本上运行任何东西,看起来表仍处于读锁定状态。

当我关闭运行笔记本的终端时,锁会释放。

运行代码示例

def met():
    con=psycopg2.connect(host=
                         ,user=
                         ,password=
                         ,port=
                         ,dbname =)
    table_data = pd.read_sql_query(query, con)
    con.close()

标签: pythonamazon-web-servicesjupyter-notebookamazon-redshiftpsycopg2

解决方案


您的光标可能保持打开状态,您应该将其包装在 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()

查看此问题以获取更多信息:将 try/except 与 psycopg2 或“关闭”一起使用?


推荐阅读