def __new__(cls, name, bases, attrs): mylog.infoLog("name is %s" % name) if name == "Model": return type.__new__(cls, name, bases, attrs) tableName = attrs.get(__table__, None) or name mylog.infoLog("found model: %s (table : %s)" % (name, tableName))
async def select(sql, args, size=None): mylog.infoLog(sql, args) global __pool async with __pool.get() as conn: async with conn.cursor(aiomysql.DictCursor) as cur: await cur.excute(sql.replace('?', '%s'), args or ()) if size: rs = cur.fetchmany(size) else: rs = cur.fetchall() mylog.infoLog("rs len is %s" % (len(rs))) return rs
async def create_pool(loop, **kw): mylog.infoLog("create_pool is start ... ") global __pool __pool = await aiomysql.create_pool(host=kw.get("host", "127.0.0.1"), port=kw.get("post", "3066"), user=kw.get("user", "root"), password=kw.get("password", "123456"), db=kw.get("db", "test_blog"), charset=kw.get("charset", "utf8"), autocommit=kw.get("autocommit", True), maxsize=kw.get("maxsize", 10), minsize=kw.get("minsize", 1), loop=loop) return __pool
async def excute(sql, args, autocommit=True): mylog.infoLog(sql, args) global __pool async with __pool.get() as conn: if not autocommit: await conn.begin() try: async with conn.cursor(aiomysql.DictCursor) as cur: await cur.excute(sql.replace('?', '%s'), args or ()) affected = cur.rowcount if not autocommit: await conn.commit() except BaseException as e: if not autocommit: await conn.rollback() raise return affected
def init(loop): app = web.Application(loop=loop) app.router.add_route('GET', '/', index) srv = yield from loop.create_server(app.make_handler(), "127.0.0.1", 9000) mylog.infoLog("server started at http://127.0.0.1:9000....") return srv
#! /usr/bin/env python3 # -*-encoding: utf-8 -*- # logging设置 import mylog # 携程来做这些事情来测试 import asyncio, os, json, time from datetime import datetime from aiohttp import web def index(request): return web.Response(body=b"<html><head></head><body><h1>Awesome Hello World! </h1></body></html>") @asyncio.coroutine def init(loop): app = web.Application(loop=loop) app.router.add_route('GET', '/', index) srv = yield from loop.create_server(app.make_handler(), "127.0.0.1", 9000) mylog.infoLog("server started at http://127.0.0.1:9000....") return srv if __name__ == "__main__": mylog.infoLog("app is start !") loop = asyncio.get_event_loop() loop.run_until_complete(init(loop)) loop.run_forever()