def creat_pool(loop, **kw): logging.info("Create database connection pool...") global __pool __pool = yield form aiomysql.create_pool( host = kw.get('host', 'localhost'), port = kw.get('port', 3306), user = kw['user'], password= kw['password'], db = kw['db'], charset = kw.get('charset', 'utf8'), autocommit=kw.get('autocommit', True), maxsize =kw.get('maxsize', 10), minsize =kw.get('minsize', 1), loop = loop )
def f(**kw): conn_kw = mysql_params.copy() conn_kw.update(kw) _loop = conn_kw.pop('loop', loop) pool = yield from aiomysql.create_pool(loop=_loop, **conn_kw) pools.append(pool) return pool
async def test_create_pool_deprecations(mysql_params, loop): async with create_pool(loop=loop, **mysql_params) as pool: with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") async with pool.get() as conn: pass assert issubclass(w[-1].category, DeprecationWarning) assert conn.closed async with create_pool(loop=loop, **mysql_params) as pool: with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") with await pool as conn: pass assert issubclass(w[-1].category, DeprecationWarning) assert conn.closed
def init(loop): db = web.conf.config.config.get('xiucai_db') pool = yield from aiomysql.create_pool( host=db.get('host', 'localhost'), port=db.get('port', 3306), user=db['user'], password=db['password'], db=db['db'], autocommit=db.get('autocommit', True), maxsize=db.get('maxsize', 10), minsize=db.get('minsize', 1), loop=loop ) with(yield from pool) as conn: cur = yield from conn.cursor(aiomysql.DictCursor) condition['recordDate'] = {"$gte": begin, "$lt": end} condition['userId'] = {"$gt": 0} t = collection.find(condition) count = 0 login_user = [] for x in t: if x.get('userId') not in login_user: login_user.append(x.get('userId')) yield from cur.execute( 'select vip_level from xc_member where ifnull(is_vest,0) = 0 and id=%s' % str(x.get( 'userId')), None) rs = yield from cur.fetchone() if rs is not None: if rs.get('vip_level') == 400: count = count + 1 print("总共会员登录数:%s" % count)
def f(**kw): nonlocal pool conn_kw = mysql_params.copy() conn_kw.update(kw) _loop = conn_kw.pop('loop', loop) pool = yield from aiomysql.create_pool(loop=_loop, **conn_kw) return pool
def create_pool(loop=None, **kw): #打印创建数据库连接日志信息: logging.info('create database connection pool...') #声明'__pool'为全局变量: global __pool #aiomysql.create_pool()创建连接到Mysql数据库池中的协程链接: __pool = yield from aiomysql.create_pool( host=kw.get('host', 'localhost'), #数据库链接地址,默认localhost port=kw.get('port', 3306), #链接端口号,默认3306 user=kw['user'], #登陆名 password=kw['password'], #登陆密码 db=kw['db'], #数据库名 charset=kw.get('charset', 'utf8'), #字符集设置,默认utf-8 autocommit=kw.get('autocommit', True), #自动提交模式,默认True maxsize=kw.get('maxsize', 10), #最大连接数,默认10 minsize=kw.get('minsize', 1), #最小连接数,默认1 loop=loop #可选循环实例,[aiomysql默认为asyncio.get_event_loop()] )
async def go(): async with create_pool(**kw) as pool: with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") async with pool.get() as conn: pass assert issubclass(w[-1].category, DeprecationWarning) assert conn.closed async with create_pool(**kw) as pool: with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") with await pool as conn: pass assert issubclass(w[-1].category, DeprecationWarning) assert conn.closed
def start(self): yield from super().start() LOG.info('Starting engines...') print('Starting engines...') self.engines['pg'] = self.loop.create_task(aiopg.create_pool(host=os.environ.get('DBHOST', self.config['engines']['pg']['host']), port=int(self.config['engines']['pg']['port']), sslmode='disable', dbname=self.config['engines']['pg']['dbname'], user=self.config['engines']['pg']['user'], password=self.config['engines']['pg']['password'], cursor_factory=psycopg2.extras.RealDictCursor, minsize=int(self.config['engines']['pg']['minsize']), maxsize=int(self.config['engines']['pg']['maxsize']), loop=self.loop)) self.engines['mysql'] = self.loop.create_task(aiomysql.create_pool( host=self.config['engines']['mysql']['host'], port=self.config['engines']['mysql']['port'], user=self.config['engines']['mysql']['user'], password=self.config['engines']['mysql']['pwd'], db=self.config['engines']['mysql']['db'], minsize=int(self.config['engines']['mysql']['minsize']), maxsize=int(self.config['engines']['mysql']['maxsize']), cursorclass=aiomysql.DictCursor, charset='utf8', use_unicode=True, loop=self.loop)) yield from asyncio.wait([self.engines['pg']], return_when=asyncio.ALL_COMPLETED) LOG.info('All engines ready !')
def init(loop): db = web.conf.config.config.get('xiucai_db') pool = yield from aiomysql.create_pool( host=db.get('host', 'localhost'), port=db.get('port', 3306), user=db['user'], password=db['password'], db=db['db'], charset='utf8', autocommit=db.get('autocommit', True), maxsize=db.get('maxsize', 10), minsize=db.get('minsize', 1), loop=loop ) with(yield from pool) as conn: cur = yield from conn.cursor(aiomysql.DictCursor) sheet_1 = read_xls_file() new_data = [] for i, row in enumerate(sheet_1): if i == 0: row.append('昵称') row.append('公司') row.append('手机号') row.append('职位') if i > 0: sql = "select cellphone,nickname,company,position from xc_member where email='%s'" % (row[0]) yield from cur.execute(sql, None) rs = yield from cur.fetchall() if isinstance(rs, list): row.append(rs[0].get('nickname')) row.append(rs[0].get('company')) row.append(rs[0].get('cellphone')) row.append(rs[0].get('position')) new_data.append(row) save_xls_file(new_data)
def connect(loop, host='localhost', port=3306, user='******', password='', db='faf_test', minsize=1, maxsize=1, cursorclass=LoggingCursor): """ Initialize the database pool :param loop: :param host: :param user: :param password: :param db: :param minsize: :param maxsize: :param cursorclass: :return: """ pool = yield from aiomysql.create_pool(host=host, port=port, user=user, password=password, db=db, autocommit=True, loop=loop, minsize=minsize, maxsize=maxsize, cursorclass=cursorclass) set_pool(pool) return pool
def connect(self): """Create connection pool asynchronously. """ self.pool = yield from aiomysql.create_pool( loop=self.loop, db=self.database, connect_timeout=self.timeout, **self.connect_kwargs)
def create_pool(db, password, user, loop, minsize=1, maxsize='10', autocommit=True, charset='utf-8', port='3306', host='localhost', **kw): logging.info('create database connection') global __pool __pool=yield from aiomysql.create_pool(host=host, port=port, user=user, password=password, db=db, charset=charset, autocommit=autocommit, maxsize=maxsize, minsize=minsize, loop=loop)
def create_pool(loop, **kw): logging.info('create database connection pool...') global __pool __pool = yield from aiomysql.create_pool( host=kw.get('host','localhost'), port=kw.get('port',3306), )
async def go(): async with create_pool(host='127.0.0.1', port=3306, user='******', password='******', db='mysql', loop=loop, autocommit=True) as pool: async with pool.get() as conn: async with conn.cursor() as cur: await cur.execute("SELECT 42;") value = await cur.fetchone() print(value)
def setUp(self): self.pool = yield from aiomysql.create_pool(host='127.0.0.1', port=3306, user='******', password='******', db='test', loop=asyncio.get_event_loop()) config = configparser.ConfigParser() config['users'] = {'foo': hashlib.sha224('pass'.encode()).hexdigest()} config['domopyc'] = {'title': 'title'} self.server = yield from domopyc_server.init(self.loop, self.pool, port=12345, config=config)
def setUp(self): self.pool = yield from aiomysql.create_pool(host='127.0.0.1', port=3306, user='******', password='******', db='test', loop=self.loop) with (yield from self.pool) as conn: cur = yield from conn.cursor() yield from cur.execute("drop table if EXISTS domopyc_switch") yield from cur.close() self.switch_service = SwichService(self.pool)
def _create_engine(minsize=1, maxsize=10, loop=None, **kwargs): if loop is None: loop = asyncio.get_event_loop() pool = yield from aiomysql.create_pool(minsize=minsize, maxsize=maxsize, loop=loop, **kwargs) conn = yield from pool.acquire() try: return Engine(_dialect, pool, **kwargs) finally: pool.release(conn)
def setUp(self): self.pool = yield from aiomysql.create_pool(host='127.0.0.1', port=3306, user='******', password='******', db='test', loop=asyncio.get_event_loop()) self.server = yield from domopyc_server.init(asyncio.get_event_loop(), self.pool, port=12345) self.message_handler = MysqlCurrentCostMessageHandler(self.pool) with (yield from self.pool) as conn: cur = yield from conn.cursor() yield from cur.execute("truncate current_cost") yield from cur.close()
def create_pools(loop, pool_size=5, **kw): global __pool try: __pool = yield from aiomysql.create_pool( maxsize = pool_size, minsize = pool_size, loop = loop, **kw ) except Exception as e: raise e
def setUp(self): self.pool = yield from aiomysql.create_pool(host='127.0.0.1', port=3306, user='******', password='******', db='test', loop=self.loop) self.message_handler = MysqlCurrentCostMessageHandler(self.pool) self.current_cost_service = CurrentCostDatabaseReader(self.pool, time(8, 0), time(22, 0)) current_cost_mysql_service.now = lambda: datetime(2015, 6, 1, 12, 0, 0, tzinfo=get_localzone()) with (yield from self.pool) as conn: cur = yield from conn.cursor() yield from cur.execute("truncate current_cost")
def setUp(self): self.pool = yield from aiomysql.create_pool(host='127.0.0.1', port=3306, user='******', password='******', db='test', loop=asyncio.get_event_loop()) with (yield from self.pool) as conn: cur = yield from conn.cursor() yield from cur.execute("drop table if EXISTS test_temp") yield from cur.close() redis_toolbox.now = lambda: datetime(2012, 12, 13, 14, 2, 0, tzinfo=timezone.utc) self.message_handler = MysqlTemperatureMessageHandler(self.pool, 'test_temp')
def go(): pool = yield from aiomysql.create_pool( host="127.0.0.1", port=3306, user="******", password="******", db="mysql", loop=loop ) with (yield from pool) as conn: cur = yield from conn.cursor() yield from cur.execute("SELECT 10") print(cur.description) (r,) = yield from cur.fetchone() assert r == 10 pool.close() yield from pool.wait_closed()
def test_example(): pool = yield from aiomysql.create_pool(host='127.0.0.1', port=3306, user='******', password='', db='mysql', loop=loop) with (yield from pool) as conn: cur = yield from conn.cursor() yield from cur.execute("SELECT 10") # print(cur.description) (r,) = yield from cur.fetchone() assert r == 10 pool.close() yield from pool.wait_closed()
def create_pool(self, no_loop=False, use_unicode=True, **kwargs): loop = None if no_loop else self.loop pool = yield from aiomysql.create_pool(loop=loop, host=self.host, port=self.port, user=self.user, db=self.db, password=self.password, use_unicode=use_unicode, **kwargs) self.pool = pool return pool
def setUp(self): self.root_conn = yield from aiomysql.connect(host='127.0.0.1', port=3306, user='******', password='******', db='test', loop=self.loop) cur = yield from self.root_conn.cursor() yield from cur.execute("SET GLOBAL wait_timeout=1") yield from cur.close() self.pool = yield from aiomysql.create_pool(minsize=2, host='127.0.0.1', port=3306, user='******', password='******', db='test', loop=self.loop) self.keep_alive = KeepAliveService(self.pool, self.loop, 1).start()
def create_app(loop): app = web.Application(loop=loop) app.router.add_route('GET', '/', hello) app['DBPOOL'] = yield from aiomysql.create_pool(host='localhost', user='******', password='******', db='test', minsize=10, maxsize=10, loop=loop) app.on_cleanup.append(on_cleanup) return app
def create_pool(loop, **kw): global __pool __pool = yield from aiomysql.create_pool( host=kw.get('host', 'localhost'), port=kw.get('port', 3306), user=kw['user'], password=kw['password'], db=kw['database'], charset=kw.get('charset', 'utf8'), autocommit=kw.get('autocommit', True), maxsize=kw.get('maxsize',10), minsize=kw.get('minsize', 1), loop=loop )
def create_pool(loop, **kwargs): logging.info('Create the database connection pool') global __pool __pool = yield from aiomysql.create_pool( host=kwargs.get('host', '127.0.0.1'), port=kwargs.get('port', 3306), user=kwargs['user'], password=kwargs['password'], db=kwargs['db'], autocommit=kwargs.get('autocommit', True), maxsize=kwargs.get('maxsize', 10), minsize=kwargs.get('minsize', 1), loop=loop )
def create_pool(loop, **kw): logging.info('create database connection pool...') global __pool __pool = aiomysql.create_pool( host = kw.get('host', 'localhost'), port = kw.get('port', 3306), user = kw['root'], password = kw['1'], db = kw['test'], charset = kw.get('charset', 'utf8'), autocommit = kw.get('autocommit', True), maxsize = kw.get('maxsize', 10), minszie = kw.get('minsze', 1) )
def create_pool(loop, **kw): # this function used to establish thread-pool global __pool # 全局变量用于保存连接池 __pool = yield from aiomysql.create_pool( host=kw.get('host', 'localhost'), # 默认定义host名字为localhost port=kw.get('port', 3306), # 默认定义mysql的默认端口号为3306 user=kw['user'], # user通过关键字传递进来 password=kw['password'], db=kw['database'], charset=kw.get('autocommit', True), # 默认自动提交事务 maxsize=kw.get('maxsize', 10), # 连接池最多10个请求 minsize=kw.get('minsize', 1), # 连接池最少1个请求 loop=loop # 传递消息循环对象loop用于异步执行 )
def create_pool(loop, **kw): logging.info('create database connection pool...') global __pool __pool = yield from aiomysql.create_pool(host=kw.get('host', 'localhost'), port=kw.get('port', 3306), user=kw['user'], password=kw['password'], db=kw['db'], charset=kw.get('charset', 'utf8'), autocommit=kw.get( 'autocommit', True), maxsize=kw.get('maxsize', 10), minsize=kw.get('minsize', 1), loop=loop)
async def put(self) -> None: """ 修改技战法 :param manager_id: :return: """ try: tid = self.get_argument("tid", strip=True) classification = self.get_argument("classification", strip=True) name = self.get_argument("tactics_name", strip=True) description = self.get_argument("describe", "", strip=True) activate_groups = self.get_arguments("activate_groups", strip=True) tcontent = self.get_arguments("tcontent", strip=True) print(tcontent, type(tcontent), ",,,") except (ValueError, KeyError, AssertionError): return await self.finish({ "code": response_code.ParameterError, "msg": "获取参数错误" }) tactics_group_relation_list = [(tid, i) for i in activate_groups] async with aiomysql.create_pool( host="192.168.80.128", port=3306, user="******", password="******", db="ceshi", autocommit=True, ) as pool: async with pool.acquire() as conn: async with conn.cursor() as cur: await cur.execute( warehouse.UPDATE_TACTICS['update_tactics'], { "tid": tid, "classification": classification, "name": name, "description": description }) # 先删除技战法和组的关系然后插入 await cur.execute( warehouse. UPDATE_TACTICS['delete_tactics_group_relation'], {"tid": tid}) print(">>>>>>") print(tactics_group_relation_list) await cur.executemany( warehouse. UPDATE_TACTICS["insert_tactics_group_relation"], tactics_group_relation_list) return await self.finish({"code": 0})
def create_pool(loop, **kw): #创建数据库连接池 logging.info('create database connection pool...') global __pool __pool = yield from aiomysql.create_pool(host=kw.get('host', '127.0.0.1'), port=kw.get('post', 3306), user=kw.get('user', ''), password=kw.get('password', ''), db=kw.get('db', ''), charset=kw.get('charset', 'utf8'), autocommit=kw.get( 'autocommit', True), maxsize=kw.get('maxsize', 10), minsize=kw.get('minsize', 1), loop=loop)
def create_pool(loop, **kwargs): logging.info('create database connection pool...') global __pool #连接池由全局变量__pool存储,类似_xxx和__xxx这样的函数或变量就是非公开的(private),不应该被直接引用 __pool = yield from aiomysql.create_pool( host=kwargs.get('host', 'localhost'), port=kwargs.get('port', 3306), user=kwargs['user'], password=kwargs['password'], db=kwargs['db'], charset=kwargs.get('charset', 'utf8'), #这个必须设置,否则,从数据库获取到的结果是乱码的 autocommit=kwargs.get( 'autocommit', True), #是否自动提交事务,在增删改数据库数据时,如果为True,不需要再commit来提交事务了 maxsize=kwargs.get('maxsize', 10), minsize=kwargs.get('minsize', 1), loop=loop)
async def query_dict_list_by_ds(p_ds, p_sql): async with create_pool(host=p_ds['ip'], port=int(p_ds['port']), user=p_ds['user'], password=p_ds['password'], db=p_ds['service'], autocommit=True) as pool: async with pool.acquire() as conn: async with conn.cursor(DictCursor) as cur: await cur.execute(p_sql) v_list = [] rs = await cur.fetchall() for r in rs: v_list.append(r) return v_list
async def main(): queue = Queue(maxsize=QUEUE_SIZE) async with aiomysql.create_pool(host='####', port=3306, user='******', password='******', db='####', loop=asyncio.get_running_loop()) as pool: conn = TCPConnector(ttl_dns_cache=3600, limit=1000) async with ClientSession(connector=conn, raise_for_status=False, read_timeout=20.0, conn_timeout=5.0) as session: await run_tasks(queue, session, pool)
def create_pool(loop, **kw): logging.info('create database connection pool...') global __pool #连接池由全局变量存储 __pool = yield from aiomysql.create_pool( host=kw.get('host', 'localhost'), #默认定义host为localhost port=kw.get('port', 3306), #默认定义mysql的端口为3306 user=kw['user'], #user,password等都通过关键字参数传入 password=kw['password'], db=kw['db'], charset=kw.get('charset', 'utf8'), #默认数据库字符集为ytf8 autocommit=kw.get('autocommit', True), #默认自动提交事务 maxsize=kw.get('maxsize', 10), #连接池最大连接数为10 minsize=kw.get('minsize', 1), #最少要求1个请求 loop=loop #传递消息循环对象用于异步执行 )
def create_pool(loop, **kw): print("create database connection pool..."); global __pool __pool = yield from aiomysql.create_pool( host=kw.get('host', 'localhost'), port=kw.get('port', 3306), user=kw.get('user','root'), password=kw.get('password','mysql'), db=kw.get('db','flytree'), charset=kw.get('charset', 'utf8'), autocommit=kw.get('autocommit', True), maxsize=kw.get('maxsize', 10), minsize=kw.get('minsize', 1), loop=loop )
def create_pool(loop, **kw): logging.info('create database connection pool...') global __pool #定义一个全局变量用于存放数据库连接池对象,在函数执行前首先执行,并且永远会被执行 __pool = yield from aiomysql.create_pool( host=kw.get('host', 'localhost'), #如果键名不存在,不想返回None就返回一个自己想要的参数localhost port=kw.get('port', 3306), user=kw['user'], password=kw['password'], db=kw['db'], charset=kw.get('charset', 'utf8'), #从数据库中获取到的数据按照UTF-8编码读取,中文不会出错 autocommit=kw.get('autocommit', True), #是否自动提交事物,默认true自动 maxsize=kw.get('maxsize', 10), minsize=kw.get('minsize', 1), loop=loop)
def createPool(loop): logging.info('create connection pool....') config = configparser.ConfigParser() config.read('./conf/application.ini') global __pool __pool = yield from aiomysql.create_pool( host=config.get('database', 'host'), port=config.getint('database', 'port'), user=config.get('database', 'user'), password=config.get('database', 'password'), db=config.get('database', 'database'), autocommit=config.getboolean('database', 'autocommit'), maxsize=10, minsize=1, loop=loop)
def create_pool(loop, **kw): logging.info('create database connection pool...') global __pool __pool = yield from aiomysql.create_pool( host=kw['host'], port=kw.get('port', 3306), user=kw.get('user', 'root'), password=kw.get('password', 'happya11'), db=kw.get('db', 'mgmtinfosystem'), charset=kw.get('charset', 'utf8'), autocommit=kw.get('autocommit', True), maxsize=kw.get('maxsize', 10), minsize=kw.get('minsize', 5), loop=loop) print(__pool)
def test_example(): pool = yield from aiomysql.create_pool(host='127.0.0.1', port=3306, user='******', passwd='', db='mysql', loop=loop) with (yield from pool) as conn: cur = conn.cursor() yield from cur.execute("SELECT 10") # print(cur.description) (r, ) = yield from cur.fetchone() assert r == 10 pool.close() yield from pool.wait_closed()
async def query_list(p_sql): async with create_pool(host=db['db_ip'], port=int(db['db_port']), user=db['db_user'], password=db['db_pass'], db=db['db_service'], autocommit=True) as pool: async with pool.acquire() as conn: async with conn.cursor() as cur: await cur.execute(p_sql) v_list = [] rs = await cur.fetchall() for r in rs: v_list.append(list(r)) return v_list
def go(): pool = yield from aiomysql.create_pool(host='127.0.0.1', port=3306, user='******', password='******', db='fq', loop=loop) with (yield from pool) as conn: cur = yield from conn.cursor() yield from cur.execute("SELECT * from user") rs = yield from cur.fetchall() for r in rs: print(r) yield from cur.close() logging.info(' rows returned: %s' % len(rs)) pool.close() yield from pool.wait_closed()
def create_pool(loop, **kw): logging.info('创建数据库连接池') global _pool _pool = yield from aiomysql.create_pool( host = kw.get('host','127.0.0.1'), port = kw.get('port', 3306), user = kw['user'], password = kw['password'], db = kw['db'], charset = kw.get('charset','utf8'), autocommit = kw.get('autocommit',True), maxsize = kw.get('maxsize',10), minsize = kw.get('minsize',1), loop = loop )
def create_pool(loop,**kw): logging.info("create datebase connection pool...") global __pool __pool = yield from aiomysql.create_pool( host = kw.get("host","localhost"), port = kw.get("port",3306), user = kw['user'], password = kw['password'], db = kw['db'], charset = kw.get('charset','utf8'), autocommit = kw.get("autocommit",True), maxsize = kw.get("maxsize",10), minsize = kw.get("maxsize",1), loop = loop )
def create_pool(loop, **kw): ##**kw是一个dict logging.info('create database connection pool...') global __pool #http://aiomysql.readthedocs.io/en/latest/pool.html __pool = yield from aiomysql.create_pool( host=kw.get('host', 'localhost'), port=kw.get('port', 3306), user=kw['user'], password=kw['password'], db=kw['db'], charset=kw.get('charset', 'utf8'), autocommit=kw.get('autocommit', True), #默认自动提交事务,不用手动去提交事务 maxsize=kw.get('maxsize', 10), minsize=kw.get('minsize', 1), loop=loop)
def create_pool(loop, **kw): # 引入关键字后不用显示import asyncio了 # 该函数用于创建连接池 global __pool # 全局变量用于保存连接池 __pool = yield from aiomysql.create_pool( host=kw.get('host', 'localhost'), # 默认定义host名字为localhost port=kw.get('port', 3306), # 默认定义mysql的默认端口是3306 user=kw['user'], # user是通过关键字参数传进来的 password=kw['password'], # 密码也是通过关键字参数传进来的 db=kw['database'], # 数据库名字,如果做ORM测试的使用请使用db=kw['db'] charset=kw.get('charset', 'utf8'), # 默认数据库字符集是utf8 autocommit=kw.get('autocommit', True), # 默认自动提交事务 maxsize=kw.get('maxsize', 10), # 连接池最多同时处理10个请求 minsize=kw.get('minsize', 1), # 连接池最少1个请求 loop=loop # 传递消息循环对象loop用于异步执行 )
def get_pool(): # host = config.get('HOST') host = '127.0.0.1' print('host is : %s ' % host) # port = config.get('PORT') port = 3306 # user = config.get('USER') user = '******' # password = config.get('PASSWORD') password = '******' # db = config.get('DB') db = 'awesome' # 获取数据库连接池的对象 pool = yield from aiomysql.create_pool(host=host, port=port, user=user, password=password, db=db) return pool
async def create_pool(loop,**kw): logging.info("reate database connection pool") global __pool __pool=yield from aiomysql.create_pool( host=kw.get("host","localhost"), port=kw.get("port",3306) user=kw["user"], password=kw["password"], db=kw["db"], charset=kw.get("charset","utf8"), maxsize=kw.get("maxsize",10), minsize=kw.get("minsze",1), loop=loop )
def create_pool(loop, **kw): logger.info('Create database connction pool...') global __pool #pdb.set_trace() __pool = yield from aiomysql.create_pool( #host = kw.get('host','127.0.0.1'), host=kw.get('host', 'localhost'), port=kw.get('port', 3306), user=kw['user'], password=kw['password'], db=kw['database'], charset=kw.get('charset', 'utf8'), autocommit=kw.get('autocommit', True), maxsize=kw.get('maxsize', 10), minsize=kw.get('minsize', 1), loop=loop)
def create_pool(loop, **kw): logging.info('create database connection pool...') #定义全局变量 global __pool __pool = yield from aiomysql.create_pool( host=kw.get('host', 'localhost'), #默认主机ip port=kw.get('port', 3306), user=kw['user'], password=kw['password'], db=kw['db'], #选择数据库 charset=kw.get('charset', 'utf8'), #设置编码 autocommit=kw.get('autocommit', True), #设置自动提交事务,默认打开 maxsize=kw.get('maxsize', 10), #设置最大连接数 minsize=kw.get('minsize', 10), #设置最小连接数 loop=loop #需要传递一个事件循环实例,若无特别声明,默认使用asyncio.get_event_loop() )
def create_pool(loop, **kw): #这里的**kw是一个dict **kw参数可以包含所有连接需要用到的关键字参数 logging.info('create database connection pool...') # 全局变量__pool用于存储整个连接池 global __pool __pool = yield from aiomysql.create_pool( host=kw.get('host', 'localhost'), #默认值localhost port=kw.get('port', 3306), user=kw['user'], password=kw['password'], db=kw['db'], charset=kw.get('charset', 'utf8'), autocommit=kw.get('autocommit', True), #默认自动提交事务,不用手动去提交事务 maxsize=kw.get('maxsize', 10), # 默认最大连接数为10 minsize=kw.get('minsize', 1), loop=loop # 接收一个event_loop实例 )
def create_pool(loop, **kw): logging.info('create database connection pool...') global __pool #全局变量控制连接池,方便其他函数调用 __pool = yield from aiomysql.create_pool( #(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值。 host=kw.get('host', 'localhost'), port=kw.get('port', 3306), user=kw['user'],#字典的取值 password=kw['password'], db=kw['db'], charset=kw.get('charset', 'utf-8'), autocommit=kw.get('autocommit', True), maxsize=kw.get('maxsize', 10), minsize=kw.get('minsize', 1), loop=loop )
def create_pool(loop, **kw): logging.info('create database connection pool...') global __pool # aiomysql为MySQL数据库提供了异步IO的驱动 __pool = yield from aiomysql.create_pool( host=kw.get('host', '193.169.100.238'),#数据库主机名 port=kw.get('port', 3306),#数据库端口,默认值为3306 user=kw['user'],#数据库登录用户名 password=kw['password'],#数据库登录用户密码 db=kw['db'],#连接数据库 charset=kw.get('charset', 'utf8'),#编码格式默认为utf8 autocommit=kw.get('autocommit', True),#自动提交设置为true maxsize=kw.get('maxsize', 10),#查询获取数据量最大值 minsize=kw.get('minsize', 1),#查询获取数据量最小值 loop=loop#循环 )
def create_pool(loop, **kw): ' 创建全局连接池,**kw 关键字参数集,用于传递host port user password db等的数据库连接参数 ' logging.info('create database connection pool...') global __pool __pool = yield from aiomysql.create_pool( host=kw.get('host', 'localhost'), port=kw.get('port', 3306), user=kw['user'], password=kw['password'], db=kw['database'], charset=kw.get('charset', 'utf8'), autocommit=kw.get('autocommit', True), maxsize=kw.get('maxsize', 10), minsize=kw.get('minsize', 1), loop=loop #需要传递一个事件循环实例,若无特别声明,默认使用asyncio.get_event_loop() )
async def main(): tornado.options.parse_command_line() # Create the global connection pool. async with create_pool(host=options.db_host, port=options.db_port, user=options.db_user, password=options.db_password, db=options.db_database, unix_socket="/var/run/mysqld/mysqld.sock") as pool: async with pool.acquire() as db: await maybe_create_tables(db) app = Application(db) app.listen(options.port) shutdown_event = tornado.locks.Event() await shutdown_event.wait()
def create_pool(loop, **kw): logging.info('create database connection pool...') global __pool # dict有个get方法,接受两个参数,如果dict中有参数1这个key,则返回对应的value值,如果没有 # 这个key,则返回参数2.例如,如果kw里有host,则返回对应的value,否则返回localhost。 __pool = yield from aiomysql.create_pool(host=kw.get('host', 'localhost'), port=kw.get('port', 3306), user=kw['user'], password=kw['password'], db=kw['db'], charset=kw.get('charset', 'utf8'), autocommit=kw.get( 'autocommit', True), maxsize=kw.get('maxsize', 10), minsize=kw.get('minsize', 1), loop=loop)
async def test1(loop): async with aiomysql.create_pool(user='******', password='******', port=3306, db='awesome', loop=loop) as pool: async with pool.acquire() as cnn: print('enter') async with cnn.cursor() as cur: await cur.execute('select * from users') r = cur.fetchall() print(r) print('exit') print('-' * 10) print(pool._closed) await pool.wait_closed()
def create_pool(loop, **kw): log('crtate database connection pool...') global __pool # 全局变量 __pool = yield from aiomysql.create_pool( ## 创建的数据连接要用的信息,注意语句之间是',' host=kw.get('host', 'localhost'), ##kw为传入的参数 port=kw.get('port', 3306), user=kw['user'], password=kw['password'], db=kw['db'], charset=kw.get('charset', 'utf8'), autocommit=kw.get('autocommit', True), maxsize=kw.get('maxsize', 10), minsize=kw.get('minsize', 1), loop=loop ##loop为传入的参数 )