【问题标题】:Jupyter notebook idle locking redshift tableJupyter notebook 空闲锁定红移表
【发布时间】:2019-07-09 15:11:42
【问题描述】:

我在 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()

【问题讨论】:

    标签: python amazon-web-services jupyter-notebook amazon-redshift psycopg2


    【解决方案1】:

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

    【讨论】:

    • “con.close()”命令是否不足以关闭连接?
    • 它关闭连接,而不是光标,并且如果您的代码在到达之前出错,则不能保证运行。关闭两者或使用答案中的链接中描述的“with”也是一种很好的风格。另见stackoverflow.com/a/39616258/7692562
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多