python sqlite并发处理,python中的SQlite WAL模式。一位作者和一位读者的并发

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?

解决方案

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.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值