-
Notifications
You must be signed in to change notification settings - Fork 0
/
corutine_async.py
87 lines (68 loc) · 2.07 KB
/
corutine_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
81
82
83
84
85
86
87
import asyncio
import psycopg2
import settings
from db import create_db
from utils import log_time, get_running_loop
DB_NAME = 'corutine_async'
def ready(conn, waiter):
loop = get_running_loop()
fileno = conn.fileno()
state = conn.poll()
if state == psycopg2.extensions.POLL_OK:
loop.remove_writer(fileno)
loop.remove_reader(fileno)
waiter.set_result(None)
elif state == psycopg2.extensions.POLL_WRITE:
loop.add_writer(fileno, ready, conn, waiter)
elif state == psycopg2.extensions.POLL_READ:
loop.remove_writer(fileno)
loop.add_reader(fileno, ready, conn, waiter)
else:
raise psycopg2.OperationalError("poll() returned %s" % state)
async def wait(conn):
waiter = get_running_loop().create_future()
ready(conn, waiter)
await asyncio.wait_for(waiter, 100)
async 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
)
await wait(aconn)
acurs = aconn.cursor()
for id in data_range:
acurs.execute(f"insert into {DB_NAME} values ({id},'{tid}');")
await wait(acurs.connection)
acurs.execute(f'select pg_sleep({sleep})')
await wait(acurs.connection)
acurs.close()
aconn.close()
@log_time
def run_ainsert():
loop = asyncio.get_event_loop()
tasks = [
loop.create_task(ainsert(range(1, 11), 1, 0.1)),
loop.create_task(ainsert(range(11, 21), 2, 0.2)),
]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
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()