File size: 837 Bytes
7982d01
 
 
 
 
 
a48b894
 
 
 
 
 
 
 
 
 
 
 
 
 
7982d01
a48b894
 
7982d01
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import asyncio
import logging

log = logging.getLogger(__name__)


async def _await(aw):
    return await aw


async def gather_results(*aws) -> list:
    """Run awaitables in parallel and return results (exceptions included, never raised).

    aiogram's bot-method objects (call.answer(), message.edit_text(), ...) are awaitable but are
    unhashable pydantic models, and asyncio.gather() puts its arguments in a dict -> TypeError
    "unhashable type". Wrapping each one in a coroutine avoids that.
    """
    return await asyncio.gather(*(_await(a) for a in aws), return_exceptions=True)


async def gather_safe(*aws) -> None:
    """Parallel Telegram calls; failures are logged, not raised."""
    for res in await gather_results(*aws):
        if isinstance(res, Exception):
            log.info("Parallel call failed: %r", res)