Home>

Recently, I began to study the topic of creating Telegram bots in Python. I started writing on pytelegrambotapi, but on many sites everyone recommends learning aiogram instead of this library. Its main advantage is asynchrony. Can someone explain to an inexperienced programmer what is the difference between an asynchronous bot and a normal one, and where it will come in handy? And is it worth learning aiogram in this case?

  • Answer # 1

    what is the difference between an asynchronous bot and a normal one

    Users send a message to the bot through special telegram clients. The bot receives the message and proceeds to execute logic that somehow depends on the message. At this stagepytelegrambotapiandAIgramare no different.

    Next. This logic may require getting some information from the database, what can be done?

    1. Submit a request and humbly wait for a response. In the meantime, the incoming message queue can build up.
    2. Send a request and process another message, but come back to this later when the result is ready.

    In the first case, the message is processed synchronously, in the second, asynchronously. This shows the key differences.

    It might be helpful to read one of my other answers about async/await: https://ru.stackoverflow.com/a/1070892/271103

  • Answer # 2

    what is the difference between an asynchronous bot and a normal one

    Users send a message to the bot through special telegram clients. The bot receives the message and proceeds to execute logic that somehow depends on the message. At this stagepytelegrambotapiandAIgramare no different.

    Next. This logic may require getting some information from the database, what can be done?

    1. Submit a request and humbly wait for a response. In the meantime, the incoming message queue can build up.
    2. Send a request and process another message, but come back to this later when the result is ready.

    In the first case, the message is processed synchronously, in the second, asynchronously. This shows the key differences.

    It might be helpful to read one of my other answers about async/await: https://ru.stackoverflow.com/a/1070892/271103

  • Answer # 3

    An asynchronous function is a function that, after being called, the application continues to run because the function returns immediately.

    Simple-Asynchrony allows you to work with a bunch of users at once, without data loss, and a long wait for a response from the bot.

    Even easier-The bot processes the incoming message and immediately "turns a blind eye" to it, moving on to the next one, if the next one needs to be processed, of course. in theory, the bot can move on to solving the next problem without solving the previous one.

    Very simple-You write with a black pen, but suddenly they give you a red one, you put the black pen away without any problems, and continue to write with red.

  • Answer # 4

    An asynchronous function is a function that, after being called, the application continues to run because the function returns immediately.

    Simple-Asynchrony allows you to work with a bunch of users at once, without data loss, and a long wait for a response from the bot.

    Even easier-The bot processes the incoming message and immediately "turns a blind eye" to it, moving on to the next one, if the next one needs to be processed, of course. in theory, the bot can move on to solving the next problem without solving the previous one.

    Very simple-You write with a black pen, but suddenly they give you a red one, you put the black pen away without any problems, and continue to write with red.