tornado異步處理mysql

import tornado.ioloop
import aiomysql
 
 
async def test():
    # 這裏的loop就是我們通過asyncio.get_event_loop()創建的,但是其實可以不傳,因爲會自動創建一個
    async with aiomysql.create_pool(host="localhost", port=3306, user="user",
                                    password="password", db="db") as pool:
        async with pool.acquire() as conn:
            async with conn.cursor() as cursor:
                await cursor.execute("select * from girl")
                data1 = await cursor.fetchone()
                data2 = await cursor.fetchall()
                print(data1)
                print(data2)
 
        pool.close()
        await pool.wait_closed()
 
 
if __name__ == '__main__':
    tornado.ioloop.IOLoop.current().run_sync(test)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章