python 流式游标读取mysql大型数据库

import asyncio
import aiomysql


async def dbdaochu(loop):
    sqlstr='sql'
    conn = await aiomysql.connect(host, username,
                           pwd, db, charset='utf8',
                                  loop=loop)
    async with aiomysql.cursors.SSCursor(conn) as cursor:
        await cursor.execute(sqlstr)
        while True:
            row = await cursor.fetchone()
            if not row:
                break

            print(row)

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(dbdaochu(loop))
    loop.close()
###########Pool版本
async def dbdaochu(loop):
    sqlstr='..........'
    pool = await aiomysql.create_pool(minsize=1, maxsize=10,host=host, user=user,
                           password=password,db=db, charset='utf8',
                                  loop=loop)

    async with pool.acquire() as conn:
        async with aiomysql.cursors.SSCursor(conn) as cur:
            await cur.execute(sqlstr)
            while True:
                row = await cur.fetchone()
                if not row:
                    break
                print(row)
    pool.close()
    await pool.wait_closed()

if __name__ == '__main__':

    loop = asyncio.get_event_loop()
    loop.run_until_complete(dbdaochu(loop))
    loop.close()

  

  

posted on 2019-04-30 14:48  hailuo  阅读(1888)  评论(0编辑  收藏  举报