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?