Отдельно разберём 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
Caramelized Onion Pasta
Ingredients:
Oven:
*300 g shallots
*300 g vegan cream cheese
*60 ml maple syrup
*200 g smoked tofu
*salt and pepper
*Oil as desired
Aside from that:
*350g pasta
*200 g dried tomatoes
*1 bunch of fresh wild garlic
*60 ml pasta water
#paste
@dishes
Rice Noodle Spring Salad
Ingredients:
*1 carrot
*100 g fresh red cabbage
*1/2 cucumber
*1/2 red onion
*1 mango
*250 g rice noodles
Peanut sauce:
*2 cm ginger
*2 cloves of garlic
*200 g peanut butter
*20 ml sesame oil
*50 ml maple syrup
*50 ml soy sauce
*150ml water
Toppings:
Chili flakes
Coriander, mint
peanuts
#paste
@dishes
You Need To Taste This Green Goddess Salad With Watermelon & Pasta
Ingredients:
*200 grams of watermelon
*100 grams of cucumber
*200 grams of pasta
Dressing:
*80 grams of spinach
*1/2 bunch basil
*1 clove of garlic
*1/2 onion
*50 grams of peas
*40ml oil
*25ml water
*1 tbsp vinegar
*1 tsp salt
*40 grams of walnuts
*juice of a lemon
*1 tbsp yeast flakes
*pinch of pepper
toppings:
Walnuts, Basil, Chili Flakes
#paste
@dishes
Pasta Alla Vodka with Pancetta🧀
Ingredients:
*350g. Rigatoni pasta (Can be replaced with penne pasta)
*2 Tbsp Olive oil
*150g. diced Pancetta
*2 Shallot onion, finely chopped
*3 Garlic cloves, finely chopped
*1/2 Tsp Red-Chilli flakes
*1/2 Tsp dried Oregano
*1/2 cup Vodka
*1 cup Tomato sauce
*1/4 cup Tomato paste with basil pesto
*2 cup Heavy cream
*1/4 cup Parmesan cheese, plus more for serving
*1 tablespoon roughly chopped fresh Basil
*2 tablespoons roughly chopped Italian Parsley
#paste
@dishes
ROASTED RED PEPPER PASTA! 🌶️
Ingredients:
* 400 g. Giant Fusilli Pasta
* 4 Red Bell Peppers
* 2 Shallot onion
* 1 head Garlic
* Olive oil
* 1 cup Heavy Cream
* Red chili flakes
* Italian herbs
* 1 tbsp Tomato paste
* 1 tbsp Butter
* Salt/Pepper
#paste
@dishes