T
T
Tayrus02022-04-14 08:26:40
Python
Tayrus0, 2022-04-14 08:26:40

Why is the bot slow?

I have a bot written in python3.9, aiogram, asynchronous code everywhere, and dp.async_task scattered, that all tasks were created asynchronously, a bot on a web hook, a database on postgresql, I use asyncpg to connect, I create a pool and use connections, but when there are a lot of users, the bot starts to blunt precisely on those buttons where there is a call to the database, I sinned on the database, set logging, and it turned out that the requests themselves are completed in 100ms, and in the application (I set the timer) 1-4s, why is that ?

I also wanted to ask how it would be better

async with self.pool.acquire() as con:
    async with con.transaction():
        res = await con.fetch(sql, user_chat_id)

Or so
async with self.pool.acquire() as con:
        res = await con.fetch(sql, user_chat_id)


30-60 asyncio tasks are created per user, and each of them has a function execution
async def is_person_id_blacklisted_user(self, user_id: int, place: str, person_id):
        sql = 'SELECT blacklisted_id FROM blacklist_persons WHERE user_id = $1 AND place = $2 AND blacklisted_id = $3 LIMIT 1'

        async with self.pool.acquire() as con:
            async with con.transaction():
                return await con.fetchval(sql, user_id, place, str(person_id))


And just like that, it starts to slow down, there are only 200k records in the table, sometimes this function returns + - a quick answer, but sometimes 1-4s, which is already too much

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
ky0, 2022-04-14
@ky0

Even 100 ms on tables of this size is a lot. The first thing that comes to mind is that the DBMS does not have enough memory / processor, or it is not configured (this also applies to the necessary indexes).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question