Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
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
#SCRT is rebounding from the support trendline of a falling wedge pattern, showing early signs of recovery.
The 50MA is acting as a resistance barrier above the wedge, limiting upside for now.
A strong breakout of the wedge would confirm bullish momentum and could trigger a solid rally.
#SCRT/USDT analysis :
#SCRT is currently trading within a channel. The price is likely to move upward and test the channel's resistance. It is advisable to wait for a pullback to the support zone for a long entry, as the price is expected to continue its upward movement.
TF : 4h
Entry : $0.1958
Target : $0.2354
SL : $0.1793
#SCRT/USDT analysis :
#SCRT is in a downtrend, consistently trading below the 200 EMA. The price is currently trading in a resistance zone.
It is expected to be rejected from there and will soon continue its downward movement. Wait for the breakout of the $0.1960 level for a short entry; the previous lows will be the target.
TF : 30min
Entry : $0.1960
Target : $0.1742
SL : $0.2094
#SCRT result
1st target achieved in just 1 hour✅
One more quick profit 5%💰🤑
👉 More quick profit signals available in premium channel. Hurry up 🏃♂👇
☎️ Contact @MichaelStrategiesVip