Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
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
#PYR/USDT analysis :
#PYR is currently in an uptrend, forming a higher highs (HHs) and higher lows (HLs) structure. At present, the price is undergoing a correction phase, with expectations of a breakout from the trendline soon, which will allow it to resume its bullish momentum. Wait for a break above the $4.320 level as a signal to enter long positions.
TF : 1h
Entry : $4.320
Target : $4.845
SL : $3.969
#PYR/USDT analysis :
#PYR is in a downtrend, forming lower lows (LLs) and lower highs (LHs) structure. The price is currently trading below the 200 Exponential Moving Average (EMA). It is anticipated that the price will retrace back to the resistance zone and then reverse from there to continue its bearish momentum and test the previous swing low.
TF : 30min
Entry : $2.438
Target : $2.299
SL : $2.495
#PYR
https://www.binance.com/en/trade/PYR_BTC
Buying Zone 1850-1868
We are expecting ride in this Coin 🚀🚀🚀🚀
Buy in parts and Strictly follow as directed 👆
Sell
🤑 2000-2050
🤑 2050- 2250
🤑 2250- 2400
🚀 2400- 2600 & above
Exit/Re-Entry point- 1800
#PYR result
2 nd target achieved in just 5 days ✅✅
One more huge quick profit 22.4%🤑💰🤑
👉 Still thinking? The more you wait more you lose profit
☎️ Contact @MichaelStrategiesVip for membership and grab next breakout signal