Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
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
#LTO/USDT analysis :
#LTO is currently rejecting from a double top resistance zone and showing bearish signs. It is expected to continue declining and test the previous swing low.
TF : 1h
Entry : $0.1124
Target : $0.1079
SL : $0.1151
#LTO | Bounced From Support Level 🧱
How did we find this detection?
1⃣ Visit Detection Analysis at CoinLegs
2⃣ S/R Analysis
3⃣ Variables:
Binance<>USDT<>1h<> Above the S/R Zone<>Weaker
4⃣ Start Analysis
That's it.
Trade easily with free advanced trading tools 💹
#LTO bounced from the Weekly Demand zone,also broken up the trendline and now we are waiting for a pullback to enter the train 🚀
❄️@signals_bitcoin_crypto❄️
❄️@Shadow_support0o❄️
#LTO result
2nd target achieved in just 5 hours ✅✅
One more quick profit 9%💰🤑
👉 The longer you wait more you lose profit. Hurry up 🏃♂
☎️ Contact @MichaelStrategiesVip
🛒Coin added - LTO Network#LTO paired with #USDT on Binance looks good for a long-term investment.
It ranks 345 in the CoinMarketCap rating.
Good fundamentals stuff are expected by the end of the year.
The coin has not pumped and is in a neat uptrend.
The price is at the exit from the resistance zone.
Price projector indicates a rise of $0.15.
Support is the $0.05-0.06 zone.
Growth in my experimental portfolio is expected from 70 to 190%.