From df0e285b6f8465eb273af50c242299c5601fa09f Mon Sep 17 00:00:00 2001 From: lizheao Date: Wed, 15 Mar 2017 14:12:37 +0800 Subject: [PATCH 1/4] Add a new example --- examples/sanic_aiomysql_with_global_pool.py | 63 +++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 examples/sanic_aiomysql_with_global_pool.py diff --git a/examples/sanic_aiomysql_with_global_pool.py b/examples/sanic_aiomysql_with_global_pool.py new file mode 100644 index 00000000..8158a87b --- /dev/null +++ b/examples/sanic_aiomysql_with_global_pool.py @@ -0,0 +1,63 @@ +# encoding: utf-8 +""" +You need the aiomysql +""" +import asyncio +import os + +import aiomysql +import uvloop +from sanic import Sanic +from sanic.response import json + +database_name = os.environ['DATABASE_NAME'] +database_host = os.environ['DATABASE_HOST'] +database_user = os.environ['DATABASE_USER'] +database_password = os.environ['DATABASE_PASSWORD'] +app = Sanic() +asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) + + +async def get_pool(*args, **kwargs): + """ + the first param in *args is the global instance , + so we can store our connection pool in it . + and it can be used by different request + :param args: + :param kwargs: + :return: + """ + args[0].pool = { + "aiomysql": await aiomysql.create_pool(host=database_host, user=database_user, password=database_password, + db=database_name, + maxsize=5)} + async with args[0].pool['aiomysql'].acquire() as conn: + async with conn.cursor() as cur: + await cur.execute('DROP TABLE IF EXISTS sanic_polls') + await cur.execute("""CREATE TABLE sanic_polls ( + id serial primary key, + question varchar(50), + pub_date timestamp + );""") + for i in range(0, 100): + await cur.execute("""INSERT INTO sanic_polls + (id, question, pub_date) VALUES ({}, {}, now()) + """.format(i, i)) + + +@app.route("/") +async def test(): + result = [] + data = {} + async with app.pool['aiomysql'].acquire() as conn: + async with conn.cursor() as cur: + await cur.execute("SELECT question, pub_date FROM sanic_polls") + async for row in cur: + result.append({"question": row[0], "pub_date": row[1]}) + if result or len(result) > 0: + data['data'] = res + return json(data) + + +if __name__ == '__main__': + app.run(host="127.0.0.1", workers=4, port=12000, before_start=get_pool) From 0ad016417179d621a80786d4a8483563a0cd875d Mon Sep 17 00:00:00 2001 From: lizheao Date: Wed, 15 Mar 2017 14:41:38 +0800 Subject: [PATCH 2/4] Change some code in sanic aiomysql code --- examples/sanic_aiomysql_with_global_pool.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/sanic_aiomysql_with_global_pool.py b/examples/sanic_aiomysql_with_global_pool.py index 8158a87b..012aeb61 100644 --- a/examples/sanic_aiomysql_with_global_pool.py +++ b/examples/sanic_aiomysql_with_global_pool.py @@ -15,23 +15,23 @@ database_host = os.environ['DATABASE_HOST'] database_user = os.environ['DATABASE_USER'] database_password = os.environ['DATABASE_PASSWORD'] app = Sanic() -asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) -async def get_pool(*args, **kwargs): +@app.listener("before_server_start") +async def get_pool(app, loop): """ - the first param in *args is the global instance , + the first param is the global instance , so we can store our connection pool in it . and it can be used by different request :param args: :param kwargs: :return: """ - args[0].pool = { + app.pool = { "aiomysql": await aiomysql.create_pool(host=database_host, user=database_user, password=database_password, db=database_name, maxsize=5)} - async with args[0].pool['aiomysql'].acquire() as conn: + async with app.pool['aiomysql'].acquire() as conn: async with conn.cursor() as cur: await cur.execute('DROP TABLE IF EXISTS sanic_polls') await cur.execute("""CREATE TABLE sanic_polls ( @@ -60,4 +60,4 @@ async def test(): if __name__ == '__main__': - app.run(host="127.0.0.1", workers=4, port=12000, before_start=get_pool) + app.run(host="127.0.0.1", workers=4, port=12000) From 97c2056e4a0511b593d0646359e9a28d72d88bd3 Mon Sep 17 00:00:00 2001 From: lizheao Date: Wed, 15 Mar 2017 14:41:54 +0800 Subject: [PATCH 3/4] Change some code in sanic aiomysql code --- examples/sanic_aiomysql_with_global_pool.py | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/sanic_aiomysql_with_global_pool.py b/examples/sanic_aiomysql_with_global_pool.py index 012aeb61..df3db8f4 100644 --- a/examples/sanic_aiomysql_with_global_pool.py +++ b/examples/sanic_aiomysql_with_global_pool.py @@ -6,7 +6,6 @@ import asyncio import os import aiomysql -import uvloop from sanic import Sanic from sanic.response import json From cfc53d0d2684d05a6f55153ad6293736892dda1f Mon Sep 17 00:00:00 2001 From: lizheao Date: Wed, 15 Mar 2017 14:42:22 +0800 Subject: [PATCH 4/4] Change some code in sanic aiomysql code --- examples/sanic_aiomysql_with_global_pool.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/sanic_aiomysql_with_global_pool.py b/examples/sanic_aiomysql_with_global_pool.py index df3db8f4..65d5832d 100644 --- a/examples/sanic_aiomysql_with_global_pool.py +++ b/examples/sanic_aiomysql_with_global_pool.py @@ -2,10 +2,10 @@ """ You need the aiomysql """ -import asyncio import os import aiomysql + from sanic import Sanic from sanic.response import json