Отдельно разберём 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
😄Draw
➖➖➖➖➖➖
🔘 Drawis commonly used as a verb that means creating a picture or image
🔜My cousin drew our family using different coloured pencils.
🔜My daughter has just learned to draw!
🔘We can also use the verb ‘draw’ to mean attracting attention - someone wanting to look at something.
🔜The amazing singer drew a huge audience.
🔜A World Cup game is guaranteed to draw a large number of viewers!
🔘 Drawas a noun can also describe competition results that are equal.
🔜The football match ended in a draw – it was 1:1.
🔜This boxer has never lost a fight - he has six wins and a draw.
#Draw👨🏫@America
➖➖➖➖➖➖➖➖➖➖➖➖
🆕 Crypto News @Money
😁 Crypto Game @Egame
🇺🇸 US News @America
🇯🇵 Japan News @Japan
🇦🇪 UAE News @Dubai
▶️ Popular Movies @Videos
😜 Best Funny Video @Funnys
⚠️ The project was suspended. Funds may be at risk.
🌟Starverse — Telegram Stars Farming and Everyday Giveaways
Earning stars has never been so exciting! #Starverse is a game where your tickets turn into real money. A project from the TgLab team, creators of the popular TON Dating mini app.
⏳Farming
Every hour you can turn on farming and take a part of the total income of the players.
Half of all revenue goes to farmers — the more tickets you have, the bigger the drop.
Farm, reinvest and invite friends — you'll get 15% of their purchases.
💰Golden Ticket
Every day at 9:00 UTC a winner is randomly selected and receives 15% of all purchases made during the day.
How to participate:
- farm at least 3 hours a day
- buy at least one ticket on the day of the drawing
💰Easy Withdraw
Got Stars? You can immediately transfer them to your Telegram account or convert to $USDT without delays.
⭐️Start farming now!
#stars#farming#draw#web3