简体   繁体   English

python 中的 SQLite WAL 模式。 一位作者和一位读者的并发

[英]SQlite WAL-mode in python. Concurrency with one writer and one reader

I'm trying to share a sqlite3 database between one writer process, and one reader process.我正在尝试在一个写入进程和一个读取进程之间共享一个 sqlite3 数据库。 However, it does not work, and it seems to me that nothing is being written in example.db.但是,它不起作用,在我看来,example.db 中没有写入任何内容。

reader.py阅读器.py

import sqlite3
from time import sleep
conn = sqlite3.connect('example.db', isolation_level=None)

c = conn.cursor()
while True:
    c.execute("SELECT * FROM statistics")
    try:
        print '**'
        print c.fetchone()
    except:
        pass
    sleep(3)

writer.py编写器.py

import sqlite3
from time import sleep
import os

if os.path.exists('example.db'):
    os.remove('example.db')

conn = sqlite3.connect('example.db', isolation_level=None)
c = conn.cursor()
c.execute('PRAGMA journal_mode=wal')
print c.fetchone()
c.execute("CREATE TABLE statistics (stat1 real, stat2 real)")

stat1 = 0
stat2 = 0.0
while True:
    stat1 += 1
    stat2 += 0.1
    cmd = "INSERT INTO statistics VALUES (%d,%f)" % (stat1, stat2)
    print cmd
    c.execute(cmd)
    #conn.commit()
    c.execute("PRAGMA wal_checkpoint=FULL")
    sleep(0.25)

I run writer.py in a background process, and then I run reader.py.我在后台进程中运行 writer.py,然后运行 ​​reader.py。 This is what it shows:这是它显示的内容:

**
(1.0, 0.1)
**
(1.0, 0.1)
**
(1.0, 0.1)

(...) (……)

What is the correct (and best) way to set this framework with one reader and one writer?用一个读者和一个作者设置这个框架的正确(和最好)方法是什么?

What do you expect?你能指望什么? You only select the first row of the table.您只选择表格的第一行。 And this is always the same row.这始终是同一行。 The "PRAGMA"-statements have no visible effects here. “PRAGMA”语句在这里没有明显的效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM