python - 应该如何在 "steady state"中使用 sqlite3?

标签 python sqlite

我的任务每隔几秒运行一次,需要读取和写入我的 sqlite3 数据库。

为避免启动 python 解释器并为每个任务打开数据库,进程始终在运行并且 sqlite3 连接永远不会关闭。

为了提高性能并允许更多并发读取,我正在使用 WAL 模式。

问题是“检查点”永远不会建立,因为连接没有关闭,所以我的 wal 文件在增长,我的性能很差。 PRAGMA wal_checkpoint(TRUNCATE); 返回的第一个值是 1,表示它被阻止完成。

让多个进程始终在同一个数据库上运行的标准方法是什么?

  1. 如果我们不使用 WAL 模式,关闭连接是否正常?
  2. 我应该定期关闭和重新打开连接吗? (例如使用计时器)
  3. 有没有办法让 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/

相关文章:

python - matplotlib rc colorcycle参数与散点图

python - 你如何让 SQLAlchemy 覆盖 MySQL "on update CURRENT_TIMESTAMP"

python - 从包含唯一分子标识符的 Fastq 文件中删除 PCR 重复项

database - SQLite3 的线程安全性如何?

ruby-on-rails - Ruby on rails - 参数数量错误(2 为 1)[Rails]

python - 来自整数列表列表的 Scipy 稀疏矩阵

python - 线程正在为 Amazon 服务的 TCP 请求留下打开的文件

android - Android 中的游标(SQLite)

java - 安卓。 SQLite 异常 : no such column _ingredients

android - 搜索 Sqlite 全文搜索没有变音符号的阿拉伯语