-
-
Save markrwilliams/54c12ebb0059c912ca1f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import random | |
import threading | |
import sqlite3 | |
class W(threading.Thread): | |
def __init__(self, connection): | |
super(W, self).__init__() | |
self.daemon = False | |
self.connection = connection | |
def run(self): | |
while True: | |
os.write(1, "w") | |
cursor = self.connection.cursor() | |
with self.connection: | |
cursor.execute( | |
''' | |
INSERT INTO T values (?) | |
''', | |
(random.randint(0, 100),)) | |
class R(threading.Thread): | |
def __init__(self, connection): | |
super(R, self).__init__() | |
self.daemon = False | |
self.connection = connection | |
def run(self): | |
while True: | |
os.write(1, "r") | |
self.connection.execute( | |
''' | |
SELECT * FROM T LIMIT 1 | |
''').fetchall() | |
connection = sqlite3.connect('/tmp/db.sqlite3', | |
check_same_thread=False) | |
connection.executescript('CREATE TABLE IF NOT EXISTS T (thing INTEGER)') | |
writers = [W(connection) for _ in range(10)] | |
readers = [R(connection) for _ in range(10)] | |
for w in writers: | |
w.start() | |
for r in readers: | |
r.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment