Search code examples
pythonredistelegram-bot

Does telegram bot need a task queue


I'm making a telegram bot, counting on traffic of about 100 users daily. Faced with the problem of the architecture of the bot. By itself, does the telegram bot process each user's requests in turn? Then do I need a queue of tasks of the "Redis" type?

I created a queue using Redis and add to it any tasks that the user calls using commands:

job = queue.enqueue(generate_key, chat_id)
 while True:
        if job.is_finished:
            job_result = job.result
        await asyncio.sleep(1)

I also tried using my bot without queuing and didn't notice any difference. My friend and I tried to call commands at the same time, and the bot with and without a queue handles the first task of calling the command faster. So I didn't notice any difference.


Solution

  • It really depends on what your message handler does and what library you use to create the bot (asynchronous or not)

    If you use an asynchronous handler, then most likely there is no difference and you will not see any delay, since this is what asynchronous Python is for. But if we say that at some point you synchronously call data from somewhere, it makes sense to use queues.

    Example 1 Here you will see no difference cause we receive messages and send answer. If all your 100 clients will send message to bot, it will take around 5 seconds to answer them all.

    @dp.message()
    async def echo_handler(message: Message) -> None: 
       // response around 5 seconds
       await make_some_http_request(...)
       await message.answer("Echo: " + message.text)
    

    Example 2 Here you will see difference cause not async http request will block context and it will take around 500 seconds to answer all clients.

    @dp.message()
    async def echo_handler(message: Message) -> None: 
       // response around 5 seconds
       make_some_http_request(...)
    

    But if your handlers with simple logic its no difference. Check more information about how async Python works.

    Actually, your approach perfect when you have really large traffic to the bot, cause you can have only one dispatcher and cant run two processes with one bot token to "start_polling".