Search code examples
pythonpython-asynciopyrogram

Prevent TimeoutError from asyncio in Python


I am building a test suite for my Telegram Bot. There is a long-running task which may take +10mins. However, after ~15s I receive a asyncio.exceptions.TimeoutError

This is a simplified code:

from tgintegration import BotController
from tgintegration import Response
from pyrogram import Client
import asyncio
from async_timeout import timeout

def create_client(session_name: str = SESSION_NAME) -> Client:
    return Client(
        name=session_name,
        api_id = os.getenv("API_ID") ,
        api_hash = os.getenv("API_HASH") 
    )

async def run_test(client: Client):
    controller = BotController(
        peer="@bot_to_test", 
        client=client,
        max_wait=5,
        wait_consecutive=5,
        raise_no_response=True
    )

    ...

    controller.max_wait=600
    controller.wait_consecutive=600
    controller.max_wait_response=600

    print("Compute with file")
    async with timeout(600):
        async with controller.collect(count=7) as response:
            await controller.send_command("compute")
    assert "Init" in response.messages[0].text


Solution

  • Finally I just needed to add the max_wait argument to the controller.collect, like this:

        ...
    
        async with controller.collect(count=7, max_wait=600) as response:
            await controller.send_command("compute")
        assert "Init" in response.messages[0].text