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

[亡魂溺海] 提交于 2020-07-08 00:39:12

问题


I'm trying to share a sqlite3 database between one writer process, and one reader process. However, it does not work, and it seems to me that nothing is being written in example.db.

reader.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

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. 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?


回答1:


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.



来源:https://stackoverflow.com/questions/30821179/sqlite-wal-mode-in-python-concurrency-with-one-writer-and-one-reader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!