1

I'm trying to make some connections to the MySQL database asynchronously, where there is a dispute and printing on the screen or result that comes first ... but I'm not able to execute ...

import asyncio
import aiomysql
import pymysql.cursors
from datetime import datetime

loop = asyncio.get_event_loop()

sql1 = 'SELECT * FROM aluguel'
sql2 = 'SELECT * FROM filme'
sql3 = 'SELECT * FROM ator'
sql4 = 'SELECT * FROM pagamento'
sql5 = 'SELECT * FROM cliente'

def getHora():
    data = datetime.now()
    hora = data.hour
    minu = data.minute
    seg = data.second
    mseg = data.microsecond
    str_hora = str(hora) + ':' + str(minu) + ':' + str(seg) + ':' + str(mseg)
    return str_hora

async def test_example():
    conn1 = await aiomysql.connect(host='',
                                port=3306,
                                user='administrator', 
                                password='',
                                db='sakila', 
                                loop=None)
    conn2 = await aiomysql.connect(host='',
                                port=3306,
                                user='administrator', 
                                password='',
                                db='sakila', 
                                loop=None)
    conn3 = await aiomysql.connect(host='',
                                port=3306,
                                user='administrator', 
                                password='',
                                db='sakila', 
                                loop=None)
    conn4 = await aiomysql.connect(host='',
                                port=3306,
                                user='administrator', 
                                password='',
                                db='sakila', 
                                loop=None)
    conn5 = await aiomysql.connect(host='',
                                port=3306,
                                user='administrator', 
                                password='',
                                db='sakila', 
                                loop=None)
    try:                             
        print(getHora())
        cur1 = await conn1.cursor()
        cur2 = await conn2.cursor()
        cur3 = await conn3.cursor()
        cur4 = await conn4.cursor()
        cur5 = await conn5.cursor()

        print('req 1',await cur1.execute(sql1))
        print('req 2',await cur2.execute(sql2))
        print('req 3',await cur3.execute(sql3))
        print('req 4',await cur4.execute(sql4))
        print('req 5',await cur5.execute(sql5))

        await cur1.close()
        await cur2.close()
        await cur3.close()
        await cur4.close()
        await cur5.close()

    finally:
        conn1.close()
        conn2.close()
        conn3.close()
        conn4.close()
        conn5.close()
        print(getHora())

loop.run_until_complete(test_example())

this was the last code I got, trying to make five connections to the bank and 5 queries but the code above always comes first .. does anyone have any idea how I can make them compete?

2
  • Hi, Thanks man, you answer resolved my problem !! Sorry my long time answer you!! Thanks Commented May 14, 2020 at 19:40
  • No problem! Please note that on StackOverflow you're supposed to accept an answer once the question is fully answered. This gives the volunteer who wrote the answer a token reputation reward, and provides future visitors information that the issue is in fact resolved. Commented May 14, 2020 at 20:23

1 Answer 1

1

To compete, you need to run the coroutines in parallel using asyncio.create_task or asyncio.gather:

async def test(reqid, sql):
    conn = await aiomysql.connect(
        host='', port=3306, user='administrator', password='', db='sakila')
    try:                             
        print(getHora())
        cur = await conn.cursor()
        print(reqid, await cur.execute(sql))
        await cur.close()
    finally:
        conn.close()
        print(getHora())

async def compete():
    await asyncio.gather(
        test('req 1', sql1),
        test('req 2', sql2),
        test('req 3', sql3),
        test('req 4', sql4),
        test('req 5', sql5),
    )

asyncio.run(compete())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.