-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy paththreaded.py
More file actions
104 lines (79 loc) · 2.47 KB
/
threaded.py
File metadata and controls
104 lines (79 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import logging
import os
import sys
import sqlite3
import threading
import time
from utils import AUTHORS_BOOKS
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s (%(threadName)-10s) %(message)s',
)
DB_FILENAME = 'books.db'
DB_IS_NEW = not os.path.exists(DB_FILENAME)
author_insert = "INSERT INTO author (name) VALUES(?);"
author_query = "SELECT * FROM author;"
book_query = "SELECT * FROM book;"
book_insert = """
INSERT INTO book (title, author) VALUES(?, (
SELECT authorid FROM author WHERE name=? ));
"""
def show_query_results(conn, query):
cur = conn.cursor()
logging.debug("beginning read")
cur.execute(query)
logging.debug("selects issued")
had_rows = False
for row in cur.fetchall():
print row
had_rows = True
logging.debug("results fetched")
if not had_rows:
print "no rows returned"
def show_authors(conn):
query = author_query
show_query_results(conn, query)
def show_books(conn):
query = book_query
show_query_results(conn, query)
def populate_db(conn):
authors = ([author] for author in AUTHORS_BOOKS.keys())
cur = conn.cursor()
logging.debug("connected")
cur.executemany(author_insert, authors)
for author in AUTHORS_BOOKS.keys():
params = ([book, author] for book in AUTHORS_BOOKS[author])
cur.executemany(book_insert, params)
logging.debug("changes made")
def writer():
logging.debug("connecting")
with sqlite3.connect(DB_FILENAME, isolation_level="EXCLUSIVE") as conn:
populate_db(conn)
logging.debug("waiting to sync")
ready.wait()
logging.debug("PAUSING")
time.sleep(3)
conn.commit()
logging.debug("CHANGES COMMITTED")
return
def reader():
with sqlite3.connect(DB_FILENAME, isolation_level="EXCLUSIVE") as conn:
logging.debug("waiting to sync")
ready.wait()
logging.debug("beginning read")
show_authors(conn)
show_books(conn)
return
if __name__ == '__main__':
if DB_IS_NEW:
print "Database does not yet exist, please run `createdb` first"
sys.exit(1)
ready = threading.Event()
threads = [
threading.Thread(name="Reader", target=reader),
threading.Thread(name="Writer", target=writer),
]
[t.start() for t in threads]
time.sleep(2)
logging.debug('sending sync event')
ready.set()
[t.join() for t in threads]