Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
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
😄Content
➖➖➖➖➖➖
🔘As an adjective, we use content to mean feeling happy and at peace with a situation.
🔜I felt content sitting by the fire, watching the rain outside.
🔘It can also mean you accept something that is out of your control.
🔜She was content with coming 3rd in the race.
🔘 As a noun, content means the ideas or information contained in something.
🔜I’m subscribed to her money-saving channel because the content is so interesting.
🔜Your writing is enjoyable but it lacks content – add more detail please.
🔘We can also use content to talk about the high or low amount of something contained in food and drink.
🔜Soya milk has a low fat content.
🔜This chocolate bar has a high sugar content.
🔘We hope the content of this video makes you feel more content with your English! And if you enjoy our content, like this video!
#Content👨🏫@America
➖➖➖➖➖➖➖➖➖➖➖➖
🆕 Crypto News @Money
😁 Crypto Game @Egame
🇺🇸 US News @America
🇯🇵 Japan News @Japan
🇦🇪 UAE News @Dubai
▶️ Popular Movies @Videos
😜 Best Funny Video @Funnys
https://secure.avaaz.org/community_petitions/ru/ministerstvo_informatsii_i_obshchestvennogo_razvit_ostanovite_veshchanie_rossiiskikh_telekanalov_na_territorii_respubliki_kazakhstan/?cibGOsb&utm_source=sharetools&utm_medium=copy&utm_campaign=petition-1427595-ostanovite_veshchanie_rossiiskikh_telekanalov_na_territorii_respubliki_kazakhstan&utm_term=cibGOsb%2Bru
Петиция за запрет РФ пропаганды на территории РК
#content
Louis William Wain, шизофрения и коты
#content
Кэмбербетч прекрасно играет аутистов и психически не стабильных - рекомендую The Electrical Life of Louis Wain