-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread_async.py
80 lines (60 loc) · 1.73 KB
/
thread_async.py
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
import select
from threading import Thread
import psycopg2
import settings
from db import create_db
from utils import log_time
DB_NAME = 'thread_async'
def wait(conn):
while True:
state = conn.poll()
if state == psycopg2.extensions.POLL_OK:
break
elif state == psycopg2.extensions.POLL_WRITE:
select.select([], [conn.fileno()], [])
elif state == psycopg2.extensions.POLL_READ:
select.select([conn.fileno()], [], [])
else:
raise psycopg2.OperationalError("poll() returned %s" % state)
def ainsert(data_range, tid, sleep):
aconn = psycopg2.connect(
database=settings.DB_NAME,
user=settings.DB_USER,
password=settings.DB_PASSWORD,
host=settings.DB_HOST,
async_=1
)
wait(aconn)
acurs = aconn.cursor()
for id in data_range:
acurs.execute(f"insert into {DB_NAME} values ({id},'{tid}');")
wait(acurs.connection)
acurs.execute(f'select pg_sleep({sleep})')
wait(acurs.connection)
acurs.close()
aconn.close()
@log_time
def run_ainsert():
t1 = Thread(target=ainsert, args=(range(1, 11), 1, 0.1))
t2 = Thread(target=ainsert, args=(range(11, 21), 2, 0.2))
t1.start()
t2.start()
t1.join()
t2.join()
def print_data():
conn = psycopg2.connect(
database=settings.DB_NAME,
user=settings.DB_USER,
password=settings.DB_PASSWORD,
host=settings.DB_HOST,
async_=0
)
curs = conn.cursor()
curs.execute(f"select * from {DB_NAME}")
for row in curs.fetchall():
print(f'id={row[0]}, tid={row[1]}')
curs.close()
conn.close()
with create_db(DB_NAME):
run_ainsert()
print_data()