Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
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
#Idiom
💢Go off on a tangent
Or
💢 Go off at a tangent
🔰If a person or piece of writing goes off on a tangent or goes off at a tangent, they start saying or thinking something that is not directly connected with what they were saying or thinking before
💠Note:-
In geometry, a tangent is a straight line which touches a curve at one point
🕸 Ex:- Our teacher would occasionally go off on a tangent and start talking about something totally unrelated to the textbook.
💠Note:-
You can use other verbs instead of go
🕸 Ex:- The book's theme wandersoff on a tangent now and then.
#Idiom
🕸People in society🕸
❄️A self-made man
♣️ A person who is rich and successful because they have worked hard, not because they were born into a rich family
❄️The chattering classes
♣️ Educated people who enjoy discussing social, political and cultural issues
❄️A second-class citizen
♣️ Someone is treated as if they are less important than others in society
❄️The grass roots
♣️ Ordinary people in a political or sporting organisation, not the leaders
❄️ The silent majority
♣️ A large number of people who do not express their opinions publicly
❄️ The (men in) grey suits
♣️ People in business or politics with a lot of influence or power, although they are not well known to the public
❄️Public enemy number one
♣️ Smth or someone that a lot of people dislike or disapprove of
❄️New kid on the block
♣️ Someone who is new in a place or organisation and has many things to learn about it (informal).
#Idiom
💢Even exchange: a trade of equal value; when you return something and take something else that costs the same price
✨ Ex: If you return that jacket and get this sweater instead, it will be an even exchange. They're both $39.99.
#Idiom
💢(To) steer clear of: to avoid, usually due to a bad experience
✨ Ex: Mark got food poisoning at O'Reilly's? We'd better steer clear of that place from now on!
#Idiom
💢(To) have the magic touch: to have talent at doing something; to be able to do something difficult.
✨ Ex: You can't open that bottle? Give it to Ivan. He usually has the magic touch.