From b67482de9b5272bf88aef59ac3c685f52fbf5771 Mon Sep 17 00:00:00 2001 From: DanielChien Date: Wed, 4 Jan 2017 23:29:09 +0800 Subject: [PATCH 1/3] add example for asyncpg --- examples/sanic_asyncpg_example.py | 64 +++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 examples/sanic_asyncpg_example.py diff --git a/examples/sanic_asyncpg_example.py b/examples/sanic_asyncpg_example.py new file mode 100644 index 00000000..01ac1984 --- /dev/null +++ b/examples/sanic_asyncpg_example.py @@ -0,0 +1,64 @@ +""" To run this example you need additional asyncpg package + +""" +import os +import asyncio + +import uvloop +from asyncpg import create_pool +import sqlalchemy as sa + +from sanic import Sanic +from sanic.response import json + +asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) + +DB_CONFIG = { + 'host': 'localhost', + 'user': 'tachien', + 'database': 'tachien' +} + +def jsonify(records): + """ Parse asyncpg record response into JSON format + + """ + return [{key: value for key, value in + zip(r.keys(), r.values())} for r in records] + +loop = asyncio.get_event_loop() + +async def make_pool(): + return await create_pool(**DB_CONFIG) + +app = Sanic(__name__) +pool = loop.run_until_complete(make_pool()) + +async def create_db(): + """ Create some table and add some data + + """ + async with pool.acquire() as connection: + async with connection.transaction(): + await connection.execute('DROP TABLE IF EXISTS sanic_post') + await connection.execute("""CREATE TABLE sanic_post ( + id serial primary key, + content varchar(50), + post_date timestamp + );""") + for i in range(0, 100): + await connection.execute(f"""INSERT INTO sanic_post + (id, content, post_date) VALUES ({i}, {i}, now())""") + + +@app.route("/") +async def handler(request): + async with pool.acquire() as connection: + async with connection.transaction(): + results = await connection.fetch('SELECT * FROM sanic_post') + return json({'posts': jsonify(results)}) + + +if __name__ == '__main__': + loop.run_until_complete(create_db()) + app.run(host='0.0.0.0', port=8000, loop=loop) From 19426444344d428ac0a59c676f5ecd4aff5a35ae Mon Sep 17 00:00:00 2001 From: DanielChien Date: Wed, 4 Jan 2017 23:30:29 +0800 Subject: [PATCH 2/3] modify config to varbles --- examples/sanic_asyncpg_example.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/examples/sanic_asyncpg_example.py b/examples/sanic_asyncpg_example.py index 01ac1984..9817ff57 100644 --- a/examples/sanic_asyncpg_example.py +++ b/examples/sanic_asyncpg_example.py @@ -14,9 +14,11 @@ from sanic.response import json asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) DB_CONFIG = { - 'host': 'localhost', - 'user': 'tachien', - 'database': 'tachien' + 'host': '', + 'user': '', + 'password': '', + 'port': '', + 'database': '' } def jsonify(records): From 5c7c2cf85e1f742b1793ec7c20f167197e917883 Mon Sep 17 00:00:00 2001 From: easydaniel Date: Wed, 4 Jan 2017 23:35:06 +0800 Subject: [PATCH 3/3] Update sanic_asyncpg_example.py Remove unused library --- examples/sanic_asyncpg_example.py | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/sanic_asyncpg_example.py b/examples/sanic_asyncpg_example.py index 9817ff57..142480e1 100644 --- a/examples/sanic_asyncpg_example.py +++ b/examples/sanic_asyncpg_example.py @@ -6,7 +6,6 @@ import asyncio import uvloop from asyncpg import create_pool -import sqlalchemy as sa from sanic import Sanic from sanic.response import json