python - 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()
解决方案
您的光标可能保持打开状态,您应该将其包装在 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 或“关闭”一起使用?
推荐阅读
- javascript - 如何在 js 中全局使用变量(跨多个文件)?
- bash - 如何使用 /bin/sh 编写 docker 命令并将结果分配给变量?
- java - 我怎样才能让微调器提示?
- angular - Observable - 如何组合数组
- maven-3 - 为什么 maven-failsafe 插件不选择集成测试
- javascript - 将同级元素与当前元素一起拖动
- python - Numpy子集和赋值
- windows - 在循环内构建自定义 POSobjects
- google-apps-script - 附加行输出“[Ljava.lang.Object;@536f3e11”而不是范围值
- google-chrome - Netflix 如何阻止用户截取 chrome 浏览器的截图?