Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import logging
- import asyncio
- import aiomysql
- from aiohttp import web
- from aiomysql.cursors import DictCursor
- logging.basicConfig(level=logging.DEBUG)
- async def index(request):
- async with request.app["mysql"].acquire() as conn:
- async with conn.cursor() as cur:
- await cur.execute("SELECT * FROM my_table")
- lines = await cur.fetchall()
- return web.Response(text='Hello Aiohttp!')
- async def get_mysql_pool(loop):
- pool = await aiomysql.create_pool(
- host="localhost",
- user="test",
- password="test",
- db="test",
- cursorclass=DictCursor,
- loop=loop
- )
- return pool
- if __name__ == "__main__":
- loop = asyncio.get_event_loop()
- mysql = loop.run_until_complete(get_mysql_pool(loop))
- app = web.Application(loop=loop, debug=True)
- app["mysql"] = mysql
- app.router.add_get("/", index)
- web.run_app(app)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement