Skip to content

Instantly share code, notes, and snippets.

@mahmoud
Forked from markrwilliams/sorrow.py
Created November 22, 2015 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mahmoud/eed20a743aacc32e4597 to your computer and use it in GitHub Desktop.
Save mahmoud/eed20a743aacc32e4597 to your computer and use it in GitHub Desktop.
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