Created
March 31, 2018 05:19
-
-
Save psiofxt/a7fe2726a500925faf8426a37eedc9d9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import asyncio | |
import uvloop | |
from asyncpg import connect, create_pool | |
from sanic import Sanic | |
from sanic.response import json, text | |
import psycopg2.pool | |
def jsonify(records): | |
return [dict(r.items()) for r in records] | |
app = Sanic(__name__) | |
TEST_SQL_QUERY = 'SELECT * FROM data;' | |
DSN = '' | |
@app.listener('before_server_start') | |
async def register_db(app, loop): | |
app.pool_1 = await create_pool( | |
DSN, loop=loop, min_size=10, max_size=10) | |
app.pool_2 = psycopg2.pool.PersistentConnectionPool( | |
dsn=DSN, minconn=10, maxconn=10) | |
async with app.pool_1.acquire() as connection: | |
await connection.execute('DROP TABLE IF EXISTS data') | |
await connection.execute("""CREATE TABLE data ( | |
id serial primary key, | |
content varchar(50), | |
post_date timestamp | |
);""") | |
for i in range(0, 1000): | |
await connection.execute(f"""INSERT INTO data | |
(id, content, post_date) VALUES ({i}, {i}, now())""") | |
@app.get('/asyncpg/select') | |
async def root_get(request): | |
async with app.pool_1.acquire() as connection: | |
results = await connection.fetch(TEST_SQL_QUERY) | |
payload = {'posts': jsonify(results)} | |
return json(payload) | |
@app.get('/psycopg2/select') | |
async def psycopg2_select(request): | |
conn = app.pool_2.getconn() | |
conn.autocommit = True | |
with conn.cursor() as curs: | |
curs.execute(TEST_SQL_QUERY) | |
columns = [x.name for x in curs.description] | |
results = curs.fetchall() | |
payload = {'posts': [{x: y for x, y in zip(columns, result)} for result in results]} | |
return json(payload) | |
if __name__ == '__main__': | |
app.run(host='127.0.0.1', port=8000, debug=False, access_log=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
200 concurrent requests on the asyncpg pool is able to complete without a problem. The psycopg2 pool will fail every time due to limitations with connections to postgres and the fact that the connections are not being awaited in a coroutine.