SELECT Asynchronous with PYTHON

Asked

Viewed 56 times

1

Hello everyone, thank you!!

I am trying to make some connections to a Mysql database in asynchronous mode where there is dispute and print on the screen the result that arrives first... but I am not able to make it work...

import asyncio
import aiomysql

loop = asyncio.get_event_loop()

async def test_example():
    pool = await aiomysql.create_pool(host='',
                                port=3306,
                                user='administrator', 
                                password='',
                                db='sakila', 
                                loop=loop)

    pool2 = await aiomysql.create_pool(host='',
                                port=3306,
                                user='administrator', 
                                password='',
                                db='sakila', 
                                loop=loop)

    async with pool2.acquire() as conn2:
        async with conn2.cursor() as cur2:
            await cur2.execute("SELECT count(*) FROM filme")
            print(await cur2.fetchall())

    async with pool.acquire() as conn:
        async with conn.cursor() as cur:
            await cur.execute("SELECT count(*) FROM aluguel")
            print(await cur.fetchall())

    pool.close()
    pool2.close()
    await pool.wait_closed()

loop.run_until_complete(test_example())

this was the last code I arrived, trying to make two connections to the bank and 2 queries but the code that is above always arrives first.. Does anyone have any idea how I can make them compete ?

  • Hello, I don’t know if you have already solved this problem, but if you remove the await when setting the variables pool and pool2, I believe it would solve.

  • I was able to resolve it in the international forum: https://stackoverflow.com/questions/61657392/select-async-with-python

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.