Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
from fastapi.concurrency import run_in_threadpool
async def execute(data: DataRequest) -> DataResponse:
try:
result = await run_in_threadpool(sync_function, data)
return DataResponse(data=result)
except Exception as e:
return DataResponse(
error=str(e),
success=False,
)
В общем работает нормально. Для всех вызовов под капотом используется общий тредпул, всё работает предсказуемо.
Но потребовалось изменить количество запускаемых в пуле потоков (по умолчанию создается 40 воркеров).
Так как дело происходит с FastAPI, делается это через lifespan используя настройки anyio:
import anyio
@asynccontextmanager
async def lifespan(app: FastAPI):
limiter = anyio.to_thread.current_default_thread_limiter()
limiter.total_tokens = 100
yield
# если вдруг нужно вернуть обратно
limiter.total_tokens = 40
Зачем менять количество воркеров?
- уменьшить, если оперативки мало (один тред занимает ~8мб)
- увеличить чтобы выдержать нагрузку
Если есть предложения получше при тех же вводных - предлагайте😉
#async
FJORDS(峡湾) 8K 60FPS
Although a sought-after destination in the summer, the Norwegian fjords lay mostly empty in the winter time. However, this time of year is no less magical. This video aim to show this magic, all in glorious 8K resolutions at 60 fps.
挪威峡湾虽然是夏季的热门目的地,但在冬季时空无一人,在一年中的这个时间的景色同样非常不可思议。此视频旨在以60 fps的出色8K分辨率显示这种魔力。
- 作者: Morten Rustad
- https://youtu.be/0pmvuKzBIqM
频道: @View_4k#8k
Hawaii in 8K ULTRA HD - Paradise of North America (60fps)
This videos you can use for learn Nature, about place, River, Country, City, etc with Ultra-HD resolution.
- 原视频: https://youtu.be/PF1EAtMswPo
- 画质(上面链接): 8K60P
- 作者: 8K World
统计: @View_4k#4k#8k
From Darkness To Light
From Darkness To Light is an 8k Time-Lapse film shot by storm chaser Ty Schmitt. All the clips in the film were captured on the Great Plains during the 2019 spring storm season. Ty traveled over 30,000 miles in 3 months chasing Mother Nature's most intense moments.
All images were shot on a Nikon D850, and edited in LRTimelapse, After Effects, and Premiere Pro.
- 原视频: https://youtu.be/MlIXpZFTBP4
- 画质(上面链接): 8K
- 作者: Ty Schmitt
统计: @View_4k#4k#8k
Hawaii in 8K ULTRA HD - Paradise of North America (60fps)
This videos you can use for learn Nature, about place, River, Country, City, etc with Ultra-HD resolution.
- 原视频: https://youtu.be/PF1EAtMswPo
- 画质(上面链接): 8K60P
- 作者: 8K World
统计: @View_4k#4k#8k
From Darkness To Light
From Darkness To Light is an 8k Time-Lapse film shot by storm chaser Ty Schmitt. All the clips in the film were captured on the Great Plains during the 2019 spring storm season. Ty traveled over 30,000 miles in 3 months chasing Mother Nature's most intense moments.
All images were shot on a Nikon D850, and edited in LRTimelapse, After Effects, and Premiere Pro.
- 原视频: https://youtu.be/MlIXpZFTBP4
- 画质(上面链接): 8K
- 作者: Ty Schmitt
统计: @View_4k#4k#8k
Frankfurt
Frankfurt is a city of great diversity. It is a metropolis of banks and big business shows and shopping on the one side, culture and traditions on the other.
- 原视频: https://youtu.be/UaveL8lp_ws
- 画质(上面链接): 8K
- 作者: Cities in 4K
统计: @View_4k#4k#8k
FJORDS(峡湾) 8K 60FPS
Although a sought-after destination in the summer, the Norwegian fjords lay mostly empty in the winter time. However, this time of year is no less magical. This video aim to show this magic, all in glorious 8K resolutions at 60 fps.
挪威峡湾虽然是夏季的热门目的地,但在冬季时空无一人,在一年中的这个时间的景色同样非常不可思议。此视频旨在以60 fps的出色8K分辨率显示这种魔力。
- 作者: Morten Rustad
- https://youtu.be/0pmvuKzBIqM
频道: @V1_BLOG#8k#view
TelegramUltraPrivateChannel #0042
My old designs in 8 Colors
>> FOR <<
#iPhone14
#iPadAir5
#8K#Desktop
----------------------
Prod. By@Hk3ToN
----------------------