Отдельно разберём 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
💧Airdrop : Proud Kitty Gang💧
📣Complete Task: ➕ 500,000 #MINT
🏆Winners: 1000 Lucky Winners.
📅End date: 20th Jun, 2022
💎Ratings: ⭐️⭐️⭐️
🔛Proud Kitty Gang Airdrop Gleam Click Here 🔛
🌀 Follow on Twitter and retweet the pinned post.
📡Enter your information to the Airdrop Gleam.
🗞Note: All airdrop steps should be completed.
🛳Business Inquiry: Admin || Admin2
🗣#MINT
Claim your drop from MINT
Deadline: February 15
What to do:
✔️Visit the website
✔️Claim your boxes.
🟠1% of the total token supply is allocated to these boxes.
🟠TGE is promised in Q1 of this year.
🟠Boxes are awarded for owning NFT Mint Genesis (also for interacting with collections on OpenSea and Blur).
Crypto Drop Club 👉 SUBSCRIBE
🆕 Mint Blockchain is now live on TokenPocket.
🟢Mint blockchain is the Native Ethereum L2 for the NFT Industry. Bringing NFT into Mass Adoption alongside Superchain🟢🔴. Powered by Optimism and nftscan_com.
🚀How to create/import a #Mint wallet on #TokenPocket Mobile Wallet and Chrome Extension Wallet?
Guide👉https://help.tokenpocket.pro/en/wallet-operation/how-to-create-a-wallet/mint
💙Download #TokenPocket, create your Mint Wallet, and stay tuned to the upcoming campaign! 🔥
👉https://tokenpocket.pro
👉https://extension.tokenpocket.pro