我的任务每隔几秒运行一次,需要读取和写入我的 sqlite3 数据库。
为避免启动 python 解释器并为每个任务打开数据库,进程始终在运行并且 sqlite3 连接永远不会关闭。
为了提高性能并允许更多并发读取,我正在使用 WAL 模式。
问题是“检查点”永远不会建立,因为连接没有关闭,所以我的 wal 文件在增长,我的性能很差。 PRAGMA wal_checkpoint(TRUNCATE);
返回的第一个值是 1,表示它被阻止完成。
让多个进程始终在同一个数据库上运行的标准方法是什么?
- 如果我们不使用 WAL 模式,关闭连接是否正常?
- 我应该定期关闭和重新打开连接吗? (例如使用计时器)
- 有没有办法让
wal_checkpoint
执行? (不知道为什么被屏蔽了)
欢迎任何其他解决方案!
最佳答案
保持连接打开是完全没问题的,但如果您对此感到担心,您可以尝试测量这两种方法的性能,这样您就有一些数据来支持您的决定。
SQLIte docs have a section regarding runaway WAL files :
In normal cases, new content is appended to the WAL file until the WAL file accumulates about 1000 pages (and is thus about 4MB in size) at which point a checkpoint is automatically run and the WAL file is recycled. The checkpoint does not normally truncate the WAL file.
A checkpoint is only able to run to completion, and reset the WAL file, if there are no other database connections using the WAL file. If another connection has a read transaction open, then the checkpoint cannot reset the WAL file because doing so might delete content out from under the reader.
您能否提供有关该系统的更多详细信息、它的运作方式以及您想要实现的目标?也许还有另一种方式。
关于python - 应该如何在 "steady state"中使用 sqlite3?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67631992/