Python asyncio event loop - python

I want to do some commands on very high requests like 10000 and above, but even when I do for like 100 commands, it blocks some of them and if I insert 100 values, 99 are added. When I add 1000 values 15 are missing and it gives the below error
import aiomysql
import asyncio
import logging
import random
import time
logging.basicConfig(level=logging.DEBUG)
print(f'Logging Enabled: Functions being defiled')
async def select(loop, sql, pool):
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute(sql)
# r = await cur.fetchone()
# print(r)
async def insert(loop, sql, pool):
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute(sql)
await conn.commit()
async def update(loop, sql, pool):
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute(sql)
await conn.commit()
async def main(loop):
start = time.time()
tasks = []
print(f'Def Main Looped Called: Mysql being tried to Login ')
pool = await aiomysql.create_pool(
host='localhost',
user='root',
password='',
db='mydatabase',
loop=loop,
minsize=50,
maxsize=500)
for x in range(100):
list = random.sample(range(0, 9), 7)
digit1 = random.sample(range(1, 6), 1)
digit2 = random.sample(range(0, 9), 1)
netno = random.sample(range(1, 4), 1)
netno2 = random.sample(range(1, 4), 1)
substatus = random.sample(range(1, 3), 1)
output = "".join(str(i) for i in list)
output1 = "".join(str(i) for i in digit1)
output2 = "".join(str(i) for i in digit2)
netoutput = "".join(str(i) for i in netno)
netoutput2 = "".join(str(i) for i in substatus)
netoutput3 = "".join(str(i) for i in netno2)
final = ('923' + output1 + output2 + output)
c1 = select(loop=cur_loop, sql="SELECT DISTINCT id, msisdn, network_type, network_name, subscription_status, package_id \
FROM testdata JOIN networkstatus ON testdata.network_type=networkstatus.network_id \
WHERE network_type=" +netoutput+ " AND subscription_status= " +netoutput2+ " \
AND package_id=" +netoutput3+ ";", pool=pool)
endtimes=time.time()
c2 = insert(loop=cur_loop, sql="INSERT INTO testdata (msisdn,network_type,subscription_status,package_id) \
VALUES (" +final+ "," +netoutput+ "," +netoutput2+ "," +netoutput+ ")", pool=pool)
endtimei=time.time()
c3 = update(loop=cur_loop, sql="UPDATE testdata SET network_type = " +netoutput+ ", subscription_status= " +netoutput2+ ",\
package_id = " +netoutput+ " WHERE network_type=" +netoutput+ " AND subscription_status= " +netoutput2+ " \
AND package_id=" +netoutput+ ";", pool=pool)
endtimeu=time.time()
print(f' Sql Connected Main Function Ended')
print('Time taken by select:', endtimes-start, 'Time taken by insert:', endtimei-start, 'Time taken by update:', endtimeu-start)
# await c1,c2,c3
tasks = [asyncio.ensure_future(c1), asyncio.ensure_future(c2), asyncio.ensure_future(c3)]
print(f'Task Line Executed')
end = time.time()
print('Total time of execution:', end-start)
return await asyncio.gather(*tasks)
if __name__ == '__main__':
print(f'Code Started: Async Event Loop')
cur_loop = asyncio.get_event_loop()
asyncio.set_event_loop(cur_loop)
print(f'Before Loop Run')
cur_loop.run_until_complete(main(cur_loop))
it select update and insert values but not complete, some values are missing as it gives the error

Related

result1 sends what's in it weird (picture below)

class EconomyCog(commands.Cog, name="Help"):
def __init__(self, bot):
self.bot = bot
#commands.command()
async def Money(self, ctx):
db = sqlite3.connect("main.sqlite")
cursor = db.cursor()
cursor.execute(f"SELECT money FROM main WHERE member_id = {ctx.message.author.id}")
result = cursor.fetchone()
if result is None:
sql = ("INSERT INTO main(member_id, money) VALUES(?,?)")
val = (ctx.message.author.id, 500)
cursor.execute(sql, val)
await ctx.send("Because you didn't have an account, I just made one for you!")
db.commit()
else:
emoji = ":dollar:"
cursor.execute(f"SELECT money FROM main WHERE member_id = {ctx.message.author.id}")
result1 = cursor.fetchone()
embed = discord.Embed(title = f"{ctx.message.author.name}'s Money", description=f"${result1} {emoji}", color = discord.Colour.random())
await ctx.send(embed=embed)
cursor.close()
db.close()
Everything works fine, but now when the amount is sent it looks like this:
when I try the {result1[1]}(where the money is at) the command doesn't even work. What am I missing?
The tuple is of length 1, so you should be using {result1[0]} rather than {result1[1]}.
Indexes start at 0, not 1:
embed = discord.Embed(
title = f"{ctx.message.author.name}'s Money",
description=f"${result1[0]} {emoji}",
color = discord.Colour.random()
)

Why Python MySQL Insert into the table not working?

I am using aiomysql and MariaDB. I can create a table or select data, but I can't insert data into the table. If you SELECT data with fetchall(), then it will show what you just inserted, but immediately delete from the database.
async def test_example(loop):
pool = await aiomysql.create_pool(host='127.0.0.1', port=3306,
user='root', password='',
db='test', loop=loop)
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute("INSERT INTO `tbl`(`id`, `val`) VALUES (37, 'z');")
print(cur.fetchall())
pool.close()
await pool.wait_closed()
loop = asyncio.get_event_loop()
loop.run_until_complete(test_example(loop))
Why?
From the PEP-249 specification:
.fetchall()
Fetch all (remaining) rows of a query result, returning them as a sequence of sequences (e.g. a list of tuples).
Since sql INSERT statement doesn't produce a result set you should try a SELECT statement before trying to obtain information from the database server.
Remove the quotes from the table and column names.
import aiomysql
import asyncio
async def select(loop, sql, pool):
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute(sql)
r = await cur.fetchone()
print(r)
async def insert(loop, sql, pool):
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute(sql)
await conn.commit()
async def main(loop):
pool = await aiomysql.create_pool(host='127.0.0.1', port=3306,
user='root', password='',
db='test', loop=loop)
c1 = select(loop=loop, sql='select * from tbl', pool=pool)
c2 = insert(loop=loop, sql="INSERT INTO tbl(id, val) VALUES (37, 'z');", pool=pool)
tasks = [asyncio.ensure_future(c1), asyncio.ensure_future(c2)]
return await asyncio.gather(*tasks)
if __name__ == '__main__':
cur_loop = asyncio.get_event_loop()
cur_loop.run_until_complete(main(cur_loop))

SELECT Async with PYTHON

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?
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())

How can I tag a member in my python discord bot

So I have this leveling system in python but I wanna add an #tag so the bot mentions the member that leveled up... But I got no idea how. I already tried multiple things but they all don't work.
from discord.ext import commands
import discord
import logging
import yaml
import sqlite3
import time
import random
import re
logging.basicConfig(level=logging.INFO)
bot = commands.Bot(command_prefix='l>', description='Hype Levels')
def threshold(n):
level_threshold = 5*(n**2)+50*n+100
return level_threshold
#bot.command(pass_context=True)
async def rank(ctx):
try:
_, member = (ctx.message.content).split(' ', 1)
member = re.sub("[^0-9]", "", member)
except:
member = ctx.message.author.id
db = sqlite3.connect('users.db')
c = db.cursor()
c.execute('SELECT user.*, (SELECT count(*) FROM users AS members WHERE members.rawexp > user.rawexp) as Rank FROM users AS user WHERE id = ?',
(ctx.message.author.id, ))
user = c.fetchone()
db.close()
rank = str(user[6] + 1)
embed = discord.Embed(title='{}\'s Information'.format(ctx.message.author.name)) \
.set_thumbnail(url=ctx.message.author.avatar_url) \
.add_field(name='Rank', value='#' + rank) \
.add_field(name='Level', value=user[2]) \
.add_field(name='EXP', value='{}/{}'.format(user[3], threshold(user[2]))) \
.add_field(name='Raw EXP', value=user[4]) \
await ctx.send(embed=embed)
#bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.author.bot:
return
if message.content.startswith('l>'):
await bot.process_commands(message)
return
db = sqlite3.connect('users.db')
c = db.cursor()
c.execute('SELECT * FROM users WHERE id= ?', (message.author.id,))
user = c.fetchone()
if user is None:
await message.channel.send('Looks like you\'re new! Welcome to level 1. Initializing player...')
c.execute('INSERT INTO users(id, name, level, exp, rawexp, time) VALUES(?,?,?,?,?,?)', (message.author.id, message.author.name, 1, 0, 0, time.time()))
db.commit()
db.close()
return
if message.author.name != user[1]:
c.execute('UPDATE users SET name = ? WHERE id= ?', (message.author.name, message.author.id))
if (time.time() - user[5]) > 60:
addedexp = random.randint(10, 25)
exp = user[3] + addedexp
rawexp = user[4] + addedexp
c.execute('UPDATE users SET exp = ?, rawexp = ?, name = ?, time = ? WHERE id= ?', (exp, rawexp, message.author.name, time.time(), message.author.id))
if (exp > threshold(user[2])):
level = user[2] + 1
c.execute('UPDATE users SET exp = ?, level = ? WHERE id= ?', (0, level, message.author.id))
await message.channel.send('Wowza! You leveled up! Your level is now **{}**.'.format(level))
db.commit()
db.close()
await bot.process_commands(message)
#bot.event
async def on_ready():
print("HypeLevels V1.1")
print("Made by: Garbodonk#6347")
print("Bot Name: " + str(bot.user.name))
print("Bot ID: " + str(bot.user.id))
await bot.change_presence(activity=discord.Streaming(name='some HYPE', url="https://www.twitch.tv/hypepixelbot", type=1))
#Initialize database.
db = sqlite3.connect('users.db')
c = db.cursor()
c.execute('CREATE TABLE IF NOT EXISTS users(id INTEGER PRIMARY KEY, name TEXT, level INT, exp INTEGER, rawexp INTEGER, time REAL)')
db.commit()
bot.run('Token')
But I want to add an #tag when people level up so they get tagged
if (exp > threshold(user[2])):
level = user[2] + 1
c.execute('UPDATE users SET exp = ?, level = ? WHERE id= ?', (0, level, message.author.id))
await message.channel.send('Wowza! You leveled up! Your level is now **{}**.'.format(level))
anyone who knows how to do this? To ask me questions or something just add me on discord: Garbodonk#6347
async def command(ctx, member : discord.Member, *, reason=None) :
await ctx.send(f" can place text here if you want {member.mention} can place text here as well")```
I hope this helps.

How can I use connection pool in aiomysql

I just don't know what to do to reuse aiomysql connection pool by reading the aiohttp examples or by google.
Here is my code
import aiomysql
import asyncio
async def select(loop, sql):
pool = await aiomysql.create_pool(host='127.0.0.1', port=3306,
user='root', password='123456',
db='test', loop=loop)
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute(sql)
r = await cur.fetchone()
print(r)
async def insert(loop, sql):
pool = await aiomysql.create_pool(host='127.0.0.1', port=3306,
user='root', password='123456',
db='test', loop=loop)
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute(sql)
await conn.commit()
async def main(loop):
c1 = select(loop=loop, sql='select * from minifw')
c2 = insert(loop=loop, sql="insert into minifw (name) values ('hello')")
tasks = [
asyncio.ensure_future(c1),
asyncio.ensure_future(c2)
]
return await asyncio.gather(*tasks)
if __name__ == '__main__':
cur_loop = asyncio.get_event_loop()
cur_loop.run_until_complete(main(cur_loop))
If i run this code, the create_pool will be executed twice.So I want to know how to change this code to reuse aiomysql connecton pool.
Thanks!
You can define pool in main func, like this:
import aiomysql
import asyncio
async def select(loop, sql, pool):
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute(sql)
r = await cur.fetchone()
print(r)
async def insert(loop, sql, pool):
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute(sql)
await conn.commit()
async def main(loop):
pool = await aiomysql.create_pool(
host='127.0.0.1',
port=3306,
user='root',
password='123456',
db='test',
loop=loop)
c1 = select(loop=loop, sql='select * from minifw limit 1', pool=pool)
c2 = insert(loop=loop, sql="insert into minifw (name) values ('hello')", pool=pool)
tasks = [asyncio.ensure_future(c1), asyncio.ensure_future(c2)]
return await asyncio.gather(*tasks)
if __name__ == '__main__':
cur_loop = asyncio.get_event_loop()
cur_loop.run_until_complete(main(cur_loop))

Categories