Skip to content

Instantly share code, notes, and snippets.

@mahmoud
Forked from markrwilliams/sorrow.py
Created November 22, 2015 02:50
Show Gist options
  • Select an option

  • Save mahmoud/eed20a743aacc32e4597 to your computer and use it in GitHub Desktop.

Select an option

Save mahmoud/eed20a743aacc32e4597 to your computer and use it in GitHub Desktop.

Revisions

  1. @markrwilliams markrwilliams created this gist Nov 22, 2015.
    54 changes: 54 additions & 0 deletions sorrow.py
    Original 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()