-
-
Save mahmoud/eed20a743aacc32e4597 to your computer and use it in GitHub Desktop.
Revisions
-
markrwilliams created this gist
Nov 22, 2015 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,54 @@ 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()