gpt4 book ai didi

python - IPython Notebook 中的 SQLAlchemy + SQLite 锁定

转载 作者:行者123 更新时间:2023-11-28 19:23:10 24 4
gpt4 key购买 nike

在 IPython notebook 实例中通过 SQLAlchemy 连接时出现OperationalError: (OperationalError) database is locked 错误,我不确定原因。

我使用 SQLAlchemy 和 Declarative Base 语法为 SQLite 数据库编写了一个 Python 接口(interface)。我将数据库模型导入到 IPython 笔记本中以探索数据。今天早上效果很好。这是代码:

from psf_database_interface import session, PSFTable
query = session.query(PSFTable).first()

但是今天下午,在我关闭运行 IPython 的笔记本电脑后(它重新启动服务器就好了),我开始收到此错误。这很奇怪,因为我仍然可以从 SQLite3 命令行工具打开数据库并查询数据。我不希望任何其他进程连接到该数据库并在数据库上运行 fuser 确认这一点。我的应用程序没有使用任何并发进程(在我编写的代码中,如果 SQLAlchemy 或 IPython 中隐藏了某些东西,则为 IDK),即使我只是在执行读取操作,SQLite 并发支持该操作。

我已经尝试重新启动 IPython 内核以及终止并重新启动 IPython 笔记本服务器。我已尝试创建数据库备份并按照此处的建议用备份替换数据库:https://stackoverflow.com/a/2741015/1216837 .最后,出于绝望,我尝试添加以下内容以查看是否可以以某种方式清除 session 中卡住的内容:

print session.is_active
session.flush()
session.close()
session.close_all()
print session.is_active

返回 TrueTrue。有什么想法吗?

更新:我可以从 python 文件运行导致错误的代码片段,没有任何问题,这个问题只发生在 IPython 中。

最佳答案

我遇到了同样的问题。我可以运行 python 脚本,但 IPython 引发以下异常。

您需要检查 fuser 没有进程正在使用它。但是,如果您找不到任何东西并且您的命令历史记录对您来说并不重要,则可以使用以下解决方法。

当我删除/home/my_user/.ipython/profile_default/history.sqlite 文件时,我可以启动IPython。正如我上面提到的,历史是空的。

    $ ipython                                                             
[TerminalIPythonApp] ERROR | Failed to create history session in /home/my_user/.ipython/profile_default/history.sqlite. History will not be saved.
Traceback (most recent call last):
File "/home/esadrfa/libs/anaconda3/lib/python3.6/site-packages/IPython/core/history.py", line 543, in __init__
self.new_session()
File "<decorator-gen-22>", line 2, in new_session
File "/home/esadrfa/libs/anaconda3/lib/python3.6/site-packages/IPython/core/history.py", line 58, in needs_sqlite
return f(self, *a, **kw)
File "/home/esadrfa/libs/anaconda3/lib/python3.6/site-packages/IPython/core/history.py", line 570, in new_session
self.session_number = cur.lastrowid
sqlite3.OperationalError: database is locked
[TerminalIPythonApp] ERROR | Failed to open SQLite history :memory: (database is locked).

关于python - IPython Notebook 中的 SQLAlchemy + SQLite 锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19963083/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com