Отдельно разберём TaskGroup, который пришел на замену gather в Python 3.11.
Ключевые отличия
▫️create_task() возвращает объект asyncio.Task, у которого есть соответствюущие методы управления. То есть у нас больше контроля
▫️это контекстный менеджер, который гарантирует что все таски будут остановлены по выходу из контекста
▫️ошибка автоматически отменяет незавершенные задачи,
▫️except* передает нам ExceptionGroup, в котором каждую ошибку можно обработать отдельно
import asyncio
import random
async def do_it() -> str:
if random.random() < 0.1:
raise ValueError('Oops')
delay = random.uniform(0.5, 1.5)
await asyncio.sleep(delay)
return delay
async def main():
try:
async with asyncio.TaskGroup() as tg:
for _ in range(10):
tasks.append(tg.create_task(do_it()))
for t in tasks:
print(t.result())
except *ValueError as e:
for err in e.exceptions:
print(err)
asyncio.run(main())
Рекомендую изучить страницу Coroutines and Tasks из документации, где представлено больше интересных примеров и механизмов
- таймауты
- отмена задач
- создание задач из другого потока
#async
✨New Character on #PixelTap, Fight with the leading meme coin on Avalanche: COQ INU
🐓 Once upon a time, in a small village, lived COQ, a poor rooster with big dreams. He made a living by selling a few eggs, hoping for a better future.
One day, COQ met a beautiful hen named Bella. They fell in love, and with Bella's care and COQ’s commitment to his work, their hens began laying the most delicious eggs. This lucky break felt like an avalanche of opportunity.
Believing in his dream, COQ created "Golden Valley Eggs," with beautiful packaging and a touching love story. People adored their eggs, and the business boomed. Overnight, COQ went from rags to riches, surprising everyone with his success.
Now, COQ is a KING, wearing fine Italian suits and a golden crown, ruling his egg empire with pride.
His journey showed that with a bit of luck and believing in your dreams, anyone can find success.
— Fight with COQ on PixelTap👈
👩🚀➖
🎁#PixelTap#Pixelverse Combo of the Day.
PixelTap has introduced the Combo of the Day, where you can earn very large rewards
🤑
https.://t.me/pixelversexyzbot👈
➖➖➖➖➖➖➖➖➖➖➖
🏆9 new promising projects👍
👉1️⃣HOT NEAR Wallet
👉2️⃣Blum
👉3️⃣MemeFi
👉4️⃣Bull Run
👉5️⃣Cityholder
👉6️⃣TimeFarm
👉7️⃣PocketFi
👉8️⃣Cyber Finance
👉9️⃣Dogshouse_bot/🆕👈
👉1️⃣0️⃣Notcoin Platinum🆕👈
➖➖➖➖➖➖➖➖➖➖➖
🎁#PixelTap#Pixelverse Combo of the Day.
PixelTap has introduced the Combo of the Day, where you can earn very large rewards
🤑
https.://t.me/pixelversexyzbot👈
➖➖➖➖➖➖➖➖➖➖➖
🏆9 new promising projects👍
👉1️⃣HOT NEAR Wallet
👉2️⃣Blum
👉3️⃣MemeFi
👉4️⃣Bull Run
👉5️⃣Cityholder
👉6️⃣TimeFarm
👉7️⃣PocketFi
👉8️⃣Cyber Finance
👉9️⃣Dogshouse_bot/🆕👈
👉1️⃣0️⃣Notcoin Platinum🆕👈
➖➖➖➖➖➖➖➖➖➖➖
🎁#PixelTap#Pixelverse Combo of the Day.
PixelTap has introduced the Combo of the Day, where you can earn very large rewards
🤑
https.://t.me/pixelversexyzbot👈
➖➖➖➖➖➖➖➖➖➖➖
🏆9 new promising projects👍
👉1️⃣HOT NEAR Wallet
👉2️⃣Blum
👉3️⃣MemeFi
👉4️⃣Bull Run
👉5️⃣Cityholder
👉6️⃣TimeFarm
👉7️⃣PocketFi
👉8️⃣Cyber Finance
👉9️⃣Dogshouse_bot/🆕👈
👉1️⃣0️⃣Notcoin Platinum🆕👈
➖➖➖➖➖➖➖➖➖➖➖
🎁#PixelTap#Pixelverse Combo of the Day.
PixelTap has introduced the Combo of the Day, where you can earn very large rewards
🤑
https.://t.me/pixelversexyzbot👈
➖➖➖➖➖➖➖➖➖➖➖
🏆9 new promising projects👍
👉1️⃣HOT NEAR Wallet
👉2️⃣Blum
👉3️⃣MemeFi
👉4️⃣Bull Run
👉5️⃣Cityholder
👉6️⃣TimeFarm
👉7️⃣PocketFi
👉8️⃣Cyber Finance
👉9️⃣Dogshouse_bot/🆕👈
👉1️⃣0️⃣Notcoin Platinum🆕👈
➖➖➖➖➖➖➖➖➖➖➖