asyncio pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost'")

Viewed 11

there is no problem with port or host since it works fine using sync way but when i try async way it's get me this error i'm new to asyncio i can't figure out where is the problem

error

    raise OperationalError(2003,
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost'")
import asyncio,aiomysql
from decouple import config

async def connect_panel():
    db = await aiomysql.connect(host="localhost",
                            user="root",
                            password="",
                            db ="panel1",autocommit=True)
    return db


async def select_panel(sql,as_dict=True) -> dict:
    connection   = await connect_panel()
    async with connection.cursor(aiomysql.DictCursor if as_dict else aiomysql.Cursor) as cu:
        await cu.execute(sql)
        result  =  await cu.fetchall()
    return result 

async def  get_scan_book():
        data =  await select_panel("SELECT id, title , book_id,slug FROM pages_books WHERE crawl = 1 AND daily = 1 ORDER BY id DESC ;",as_dict=True)
        return data

async def get_scan_as():
    data =  await get_scan_book()
    await print(data[0]["id"])
    return data

async def main():
    tasks =  list()
    for x in range(1000):
        tasks.append(asyncio.create_task(get_scan_as()))
    await asyncio.gather(*tasks)

if __name__ == "__main__":
    asyncio.run(main())


0 Answers
Related