Отдельно разберём 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
#HawkerHeritage📢 Originally the Seng Poh Road Market in 1951, Tiong Bahru Market served as a bustling centre for the community. Next time you’re there for a meal, don’t forget to soak up the history & nostalgia!
#OurHawkerCulture#OurHawkerCentres
#HawkerHeritage Fancy A Feast in a Garden 🌴? Since 1971, Newton Food Centre hawker has been serving up Singapore’s culinary diversity with its unique 'garden city' charm. Forward this post to someone who’ll appreciate this picturesque setting!
#OurHawkerCulture#OurHawkerCentres#HawkerHeritage
#HawkerHeritage📜 Tekka Centre: Let’s time-travel back to 1915, where 'Kandang Kerbau' and ‘Tekka’ tell tales of cattle and wild bamboo. What a journey for the senses!
#OurHawkerCulture#OurHawkerCentres
#HawkerHeritage🌟 People’s Park Food Centre: A Culinary Heritage Site 🌟 Starting as a hawker shelter in 1923 and evolving into the largest market by 1940, it's a place where history and cuisine meet. Share this with someone that you’ll like to explore this iconic food haven with!
#OurHawkerCulture#OurHawkerCentres
#HawkerHeritage🍲✨ Experience a rich blend of history and culinary excellence at Singapore's largest hawker centre: Chinatown Complex Market! Established in 1981 for the last of Chinatown’s street hawkers, it's now a vibrant gastronomic haven with around 700 stalls. Share this with your favourite foodies!
#OurHawkerCulture#OurHawkerCentres
#HawkerHeritage Changi Village Hawker Centre: From a military past to a seaside food haunt 🌴#DidYouKnow This dining spot was part of Changi's transformation into a resort? An idyllic memory to savour with your favourite hawker delights!
#OurHawkerCulture#OurHawkerCentres
#HawkerHeritage Architectural elegance meets culinary delight at Old Airport Road Food Centre 🍴🏛️#DidYouKnow that its modern structure was designed in '73 to match the housing estates in the area?
#OurHawkerCulture#OurHawkerCentres
#HawkerHeritage#DidYouKnow Adam Road Food Centre is nine-sided and almost circular? This popular spot is famous for its nasi lemak and a variety of delightful treats! This is your sign to organise that makan trip🍴
#OurHawkerCulture#OurHawkerCentres
❤️#HawkerHeritage❤️#DidYouKnow that Maxwell Food Centre was once a wet market? It was also one of the places where the poor and displaced could find cheap meals after WWII. Today, it’s a treasure trove of local delights!
#OurHawkerCulture#OurHawkerCentres