S
S
sovsemnevajno2021-11-03 15:15:01
MySQL
sovsemnevajno, 2021-11-03 15:15:01

Why does the telegram bot take a long time to respond to commands?

Tell me why the bot can take a long time to respond to the command. After pressing the button, it takes ~ 1 second before the bot gives a response.

It seems that it connects to the database every time before giving a response. (perhaps it is)

In terms of connection, I use aiomysql.

import logging
import aiogram
import config
import importlib
import asyncio
import aiomysql
from config import DB_CONFS
from aiogram import Bot, Dispatcher, executor, types

# log level
logging.basicConfig(level=logging.INFO)

# bot init
bot = Bot(token=config.token)
dp = Dispatcher(bot=bot)
sync_tg = SyncTG(config.token)

queue = []


async def creatPool() -> aiomysql.Pool:
    pool = await aiomysql.create_pool(host=DB_CONFS["host"], port=DB_CONFS["port"],
                                      user=DB_CONFS["user"], password=DB_CONFS["passwd"],
                                      db=DB_CONFS["database"], loop=loop, autocommit=True, pool_recycle=120)


    return pool


@dp.message_handler(content_types="text", text=" Топ")
async def top(message: types.Message):
    async with pool.acquire() as conn:
        async with conn.cursor() as cursor:
            try:
                await cursor.execute(f"SELECT status FROM users WHERE tguserid='{message.chat.id}'")
                st = await cursor.fetchall()
                if st[0][0] == 4:
                    await message.answer('⚠️ <b>Доступ запрещён</b> ⚠️', parse_mode='HTML')
                else:
                    top_d = ""
                    await cursor.execute("SELECT * FROM users WHERE status = 1 ORDER BY all DESC LIMIT 10")
                    users = await cursor.fetchall()
                    num = 1
                    for user in users:
                        top_d += f"<b>{num}. @{user[2]}</b> - <b>{user[3]}</b> \n"
                        num = num + 1
                    await message.answer('<b> Топ 10:</b>\n\n' + top_d, parse_mode='HTML')
            except:
                pass


loop = asyncio.get_event_loop()
pool: aiomysql.Pool = loop.run_until_complete(creatPool())

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-11-03
@Vindicar

async with pool.acquire() as conn:
async with conn.cursor() as cursor:
What do you think these lines do?

It seems that it connects to the database every time before giving a response.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question