python中的SQlite WAL模式。一位作者和一位读者的并发 [英] SQlite WAL-mode in python. Concurrency with one writer and one reader

查看:282
本文介绍了python中的SQlite WAL模式。一位作者和一位读者的并发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在一个写入器进程和一个读取器进程之间共享一个sqlite3数据库。但是,它不起作用,在我看来,example.db中什么也没写。

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

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

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)

我在后台进程中运行writer.py,然后运行reader.py。它显示了以下内容:

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?

推荐答案

您期望什么?您只选择表的第一行。并且这总是同一行。 PRAGMA语句在这里没有可见的效果。

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.

这篇关于python中的SQlite WAL模式。一位作者和一位读者的并发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆