29 lines
642 B
Python
29 lines
642 B
Python
import asyncio
|
|
|
|
|
|
async def risky(n: int) -> int:
|
|
if n % 2 == 0:
|
|
return n
|
|
else:
|
|
raise ValueError(f"{n} is not even")
|
|
|
|
|
|
async def main() -> None:
|
|
results_gather = await asyncio.gather(
|
|
risky(2), risky(3), risky(4), return_exceptions=True
|
|
)
|
|
print(results_gather)
|
|
|
|
try:
|
|
async with asyncio.TaskGroup() as tg:
|
|
t1 = tg.create_task(risky(5))
|
|
t2 = tg.create_task(risky(6))
|
|
t3 = tg.create_task(risky(7))
|
|
|
|
print(t1.result(), t2.result(), t3.result())
|
|
except ExceptionGroup as e:
|
|
print(e)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|