![](/img/trans.png)
[英]write csv rows one by one rather than all at once in python3 using writer.writerows(reader)
[英]SQlite WAL-mode in python. Concurrency with one writer and one reader
我正在嘗試在一個寫入進程和一個讀取進程之間共享一個 sqlite3 數據庫。 但是,它不起作用,在我看來,example.db 中沒有寫入任何內容。
閱讀器.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)
編寫器.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。 這是它顯示的內容:
**
(1.0, 0.1)
**
(1.0, 0.1)
**
(1.0, 0.1)
(……)
用一個讀者和一個作者設置這個框架的正確(和最好)方法是什么?
你能指望什么? 您只選擇表格的第一行。 這始終是同一行。 “PRAGMA”語句在這里沒有明顯的效果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.