summaryrefslogtreecommitdiff
path: root/sqlplain/doc/threadlocal_ex.py
blob: 980be1c553d41de78319f4705786a418f9c98112 (plain)
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
import threading
from sqlplain import connect, do
from sqlplain.util import create_db

create_book = do('''
CREATE TABLE book(
  title VARCHAR(64),
  author VARCHAR(32),
  rdate DATE)
''')

select_author = do('''
SELECT author FROM book
''')

insert_author = do('''
INSERT INTO book VALUES ('No title', ?, '2008-11-22')
''')

def show_authors(db):
    thread = threading.currentThread().getName()
    print('Printing from %s' % thread)
    insert_author(db, thread)
    for i, author in enumerate(select_author(db)):
        print(i, author[0])
    print('---------------------')
    
def run_many(N, action, db):
    threads = [threading.Thread(None, lambda : action(db)) for _ in range(N)]
    try:
        for th in threads:
            th.start()
    finally:
        th.join()
    
if __name__ == '__main__':
    db = create_db('sqlite:///tmp.sqlite', force=True, threadlocal=True)
    create_book(db)
    run_many(10, show_authors, db)