Отдельно разберём 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
😄Pretty
➖➖➖➖➖➖
🔘Pretty as an adjective means 'attractive, especially when talking about girls or women'.
Margo always tells her daughter that she's pretty.Margo always tells her daughter that she's pretty.
🔜Jacob's mum is really pretty.
🔘Pretty is also used to talk about things that are 'pleasant to look at in a delicate or charming way'. While this is often connected to females, it can also be used to describe something like 'a view'.
🔜There's a very pretty view at the top of that hill.
🔜My friend moved out of the city and bought a pretty cottage in the countryside.
🔘As an adverb, pretty can be an informal way of saying 'quite' or 'rather'.
🔜The house was built recently, it's pretty new.
🔜I enjoyed that film, it was pretty good.
🔘We can also use pretty to give emphasis.
🔜We went to bed at 2am, so we were pretty tired.
🔜I'm pretty angry right now, so don't talk to me.
#Pretty👨🏫@America
➖➖➖➖➖➖➖➖➖➖➖➖
🆕 Crypto News @Money
😁 Crypto Game @Egame
🇺🇸 US News @America
🇯🇵 Japan News @Japan
🇦🇪 UAE News @Dubai
▶️ Popular Movies @Videos
😜 Best Funny Video @Funnys
😄Pretty
➖➖➖➖➖➖
🔘Pretty as an adjective means 'attractive, especially when talking about girls or women'.
Margo always tells her daughter that she's pretty.Margo always tells her daughter that she's pretty.
🔜Jacob's mum is really pretty.
🔘Pretty is also used to talk about things that are 'pleasant to look at in a delicate or charming way'. While this is often connected to females, it can also be used to describe something like 'a view'.
🔜There's a very pretty view at the top of that hill.
🔜My friend moved out of the city and bought a pretty cottage in the countryside.
🔘As an adverb, pretty can be an informal way of saying 'quite' or 'rather'.
🔜The house was built recently, it's pretty new.
🔜I enjoyed that film, it was pretty good.
🔘We can also use pretty to give emphasis.
🔜We went to bed at 2am, so we were pretty tired.
🔜I'm pretty angry right now, so don't talk to me.
#Pretty👨🏫@America
➖➖➖➖➖➖➖➖➖➖➖➖
🆕 Crypto News @Money
😁 Crypto Game @Egame
🇺🇸 US News @America
🇯🇵 Japan News @Japan
🇦🇪 UAE News @Dubai
▶️ Popular Movies @Videos
😜 Best Funny Video @Funnys
http://www.enlistq.com/10-python-idioms-to-help-you-improve-your-code/
If you have ever tried to learn a new language (not a programming language), you know that we always think in our native language before we translate it to the new language. This can lead to you forming some sentences that don’t make sense in the new language but are perfectly normal in your native language. For example, in a lot of languages, you ‘open’ an electronic gadget such as fan, AC or cell phone. When you say that in English, it means to literally open the gadget instead of turning it on.
The same is true for programming languages. As we pick up new languages, such as #python, we are using our prior knowledge of programming in another language (q, java, c++ etc) and translating that to python. Many times, your code will work but it won’t be ‘#pretty’ or #fast. In python terms, your code won’t be ‘#pythonic’.