Отдельно разберём 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
'Our children are next' fear #Kenyans as #drought wipes out livestock
In drought-hit northeastern Kenya, villagers have been forced to drag their dead livestock to distant fields for burning to keep the stench of death and scavenging hyenas away from their homes.
Mandera county along Kenya's borders with Ethiopia and Somalia has seen no rain since May and is now on the point of a full-blown water emergency.
"I have lost all my cows and goats, and burned them here," Bishar Maalim Mohammed, 60, a resident of Tawakal village, told AFP.
In his village, where most are pastoralists relying heavily on their animals, the only remaining bull can no longer stand. He has lain in the same spot for nearly a week, severely dehydrated with bones protruding through his skin, as his owner watches helplessly.
https://addisstandard.com/?p=55089
#China executes 11 gang members who lured #Kenyans to #Myanmar scam compounds
China has executed 11 members of a criminal gang convicted of luring foreigners—including Kenyans—into scam compounds in Myanmar, where they ran a billion-dollar online fraud and illegal gambling network.
The criminal syndicate was convicted of running a vast online fraud and illegal gambling operation that generated more than $14 billion, according to state media and court records.
The executions approved by China's Supreme Peoples Court and carried out in eastern #Zhejiang province underscore Beijing' intensifying crackdown on transnational crime networks operating across Southeast Asia, These networks have increasingly affected victims and financial systems far beyond the region including in Africa.
Chinese authorities said the defendants were key figures in the so-called Ming family criminal group .....
https://web.facebook.com/AddisstandardEng/posts/pfbid0Txiw6s8otRMFRWz7hk7ioaecYZkBNx1cMqVXPy67uL3pPMJaJZA2fGQyqs8aymkSl