Функция asyncio.wait() это еще один способ вызвать множество асинхронных задач.
Она работает в нескольких режимах.
1. Самый простой - ждем завершения всех задач
async def main():
tasks = [asyncio.create_task(do_it(i)) for i in range(10)]
done, pending = await asyncio.wait(
tasks,
return_when=asyncio.ALL_COMPLETED
)
for task in done:
try:
print(task.result())
except Exception as e:
print(e)
Очень похоже на gather, но работает не так.
▫️возвращает не результаты, а два сета с объектами Task у которых можно забрать результат через task.result() если они в списке done
▫️не гарантирует порядок результатов так как оба объекта это set
▫️не выбрасывает исключение когда оно появляется, а сохраняет его в Task. Исключение появится когда попробуете забрать резултьтат.
2. Ждем завершения первой задачи, даже если там ошибка.
async def main():
tasks = [asyncio.create_task(do_it(i)) for i in range(3)]
done, pending = await asyncio.wait(
tasks,
return_when=asyncio.FIRST_COMPLETED
)
# в done может быть несколько задач!
for task in done:
try:
print(task.result())
except Exception as e:
print(f"Fail: {e}")
# Оставшиеся задачи в pending, как правило, нужно отменить, иначе они будут продолжать работать
for task in pending:
task.cancel()
В сете done будут таски которые успели завершится, причем как успешно так и нет.
3. До первой ошибки.
Тоже самое, но с аргументом FIRST_EXCEPTION
done, pending = await asyncio.wait(
tasks,
return_when=asyncio.FIRST_EXCEPTION
)
Функция завершается как только первая задача упадет с ошибкой.
Учтите, что в любом случае done вы можете обранужить несколько задач, как с ошибками так и успешные.
↗️ Полный листинг примеров здесь
#async
http://aiohttp.readthedocs.io/en/stable/web.html#aiohttp-web-websockets
In order to implement a #web_server, first create a #request handler.
A request handler is a coroutine or regular function that accepts a Request instance as its only parameter and returns a Response instance:
#aiohttp#asyncio
from aiohttp import web
async def hello(request):
return web.Response(text="Hello, world")
Next, create an Application instance and register the request handler with the application’s #router on a particular HTTP method and path:
https://github.com/aio-libs/aiohttp/blob/master/docs/web_reference.rst
Server Reference
#asyncio#aiohttp#Request#BaseRequest#Server#client#Router#Resource
#typescript#ai_gateway#gateway#generative_ai#hacktoberfest#langchain#llama_index#llmops#llms#openai#prompt_engineering#router
The AI Gateway by Portkey lets you connect to over 1600 AI models quickly and securely through one simple API, making it easy to integrate any language, vision, or audio AI model in under two minutes. It ensures fast responses with less than 1ms latency, automatic retries, load balancing, and fallback options to keep your AI apps reliable and scalable. It also offers strong security with role-based access, guardrails, and compliance with standards like SOC2 and GDPR. You can save costs with smart caching and optimize usage without changing your code. This helps you build powerful, cost-effective, and secure AI applications faster and with less hassle.
https://github.com/Portkey-AI/gateway
YouTube Issues and Economic Updates
🔧 Users in Russia report ongoing issues with YouTube, marking another decrease in platform traffic, as confirmed by Google.
📊 The Russian Communications Ministry (RKN) plans to acquire data on user attempts to access blocked sites, though it already collects some relevant data.
⚙️ The Ministry of Economic Development aims to increase processing limits to enhance labor market flexibility amid personnel shortages.
📈 Predictions suggest the information security market in 2024 could grow by 30% to reach 593 billion rubles, though other estimates are lower.
📺 Yandex is negotiating with Haier, TCL, and Huawei for the installation of its OS on all their TVs supplied to Russia.
💰 AI search engine Perplexity successfully raised $500 million at a valuation of $9 billion, a significant increase from its earlier valuation of $1 billion.
🇺🇸 In the US, an investigation has been initiated against TP-Link over national security concerns, as they hold 65% of the domestic router market.
#YouTube#RKN#EconomicDevelopment#MarketGrowth#InformationSecurity#Yandex#Perplexity#Funding#TPLink#NationalSecurity#Russia#TechNews#AI#Router#Television#DataPrivacy#UserExperience#TrafficIssues