Функция asyncio.wait() это еще один способ вызвать множество асинхронных задач.
Она работает в нескольких режимах.
1. Самый простой - ждем завершения всех задач
async def main():
tasks = [asyncio.create_task(do_it(i)) for i in range(10)]
done, pending = await asyncio.wait(
tasks,
return_when=asyncio.ALL_COMPLETED
)
for task in done:
try:
print(task.result())
except Exception as e:
print(e)
Очень похоже на gather, но работает не так.
▫️возвращает не результаты, а два сета с объектами Task у которых можно забрать результат через task.result() если они в списке done
▫️не гарантирует порядок результатов так как оба объекта это set
▫️не выбрасывает исключение когда оно появляется, а сохраняет его в Task. Исключение появится когда попробуете забрать резултьтат.
2. Ждем завершения первой задачи, даже если там ошибка.
async def main():
tasks = [asyncio.create_task(do_it(i)) for i in range(3)]
done, pending = await asyncio.wait(
tasks,
return_when=asyncio.FIRST_COMPLETED
)
# в done может быть несколько задач!
for task in done:
try:
print(task.result())
except Exception as e:
print(f"Fail: {e}")
# Оставшиеся задачи в pending, как правило, нужно отменить, иначе они будут продолжать работать
for task in pending:
task.cancel()
В сете done будут таски которые успели завершится, причем как успешно так и нет.
3. До первой ошибки.
Тоже самое, но с аргументом FIRST_EXCEPTION
done, pending = await asyncio.wait(
tasks,
return_when=asyncio.FIRST_EXCEPTION
)
Функция завершается как только первая задача упадет с ошибкой.
Учтите, что в любом случае done вы можете обранужить несколько задач, как с ошибками так и успешные.
↗️ Полный листинг примеров здесь
#async
☑️☑️Below you will see the Futures Premium Signals Results for (09th April 2026)
🚀#AIA-USDT - 111% profit
🚀#4-USDT - 154% profit
✅✅Net Profit = 264% Profit
👇🏻Signals Summary
➕Total Signals Sent out - 02
✔️Profitable Signals - 02
✖️Total Signals Lost - 00
◼️Trade cancelled without being executed - 00
✅✅Net Profit - 264% Profit
👁🗨Contact @futurechief to enter the most profitable Futures Premium Signal Group
🎯🎯 Non Stop Huge profit on Premium signals for all Premium Members
🔥🔥#AIA/USDT has crossed all the profit targets and made a great profit of 111%
👁🗨Contact @futurechief to enter the Premium Futures & SPOT Group for daily gains
🚀🚀Raining Profits in the Premium Group
🎯🎯#AIA/USDT has covered all the targets to give an amazing profit of 70% for all Premium Members
👁🗨Contact @futurechief to enter the premium group & make daily gains on Futures as well as Spot Market
🚀🚀🚀160%+ Huge Profit on #AIA/USDT as it has hit all the profit target with great easiness - The trade has been closed today
👁🗨Contact @primemod to enter the premium group & make daily gains
✅✅ 53% Profit on #AIA/USDT for our Premium Members on Futures, OKX, Bybit, Kucoin
🔥🔥 All target completed
👁🗨Contact @primemod to enter the Premium Binance Futures/Bybit/Kucoin/OKX & SPOT Group for daily gains