Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
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
#GMT/USDT analysis :
Following the breakout and subsequent retest of the 200 EMA, the price is now declining. It is anticipated that this downward momentum will persist, leading to a test of lower levels.
TF : 4H
Entry : $0.1586
Target : $0.1414
SL : $0.1670
#GMT/USDT analysis :
#GMT is in an uptrend on LTF. The price has broken and retested the 200 EMA. Currently, the price is bouncing back from the 200 EMA and is expected to continue its bullish momentum.
TF : 30min
Entry : $0.1132
Target : $0.1199
SL : $0.1089
#GMT overall trend is bearish, with a significant downtrend visible from the peak in late December.Price has been making lower highs and lower lows, indicating continuous selling pressure. 🔥👍✅
Further,As long as the price stays below $0.0847, the bearish trend remains strong.A potential relief bounce could occur, but confirmation is needed.Bulls need to reclaim resistance levels above $0.0847 to signal a possible trend reversal.👍🔥✅
#GMT is testing the major support zone and testing the Daily range low. Market is taking a correction before going up and we can see big moves in coming months.🔥✅
Further. GMT is looking good for investment in upcoming Alt Season.🔥✅😍👀✅🔥😳
#GMT will test the resistance zone and it has caused the liquidity on resistance zones .Market can see a breakout and bullish move soon,🔥✅
Further, market will take a bullish soon ahead as BTC.D will soon take a breakdown.🔥✅😍👀✅🔥😳
🔵#GMT price is rising in a Bullish channel on 6H Time frame,we expect it will have another bullish leg from the bottom of this pattern 👀
❄️@signals_bitcoin_crypto❄️
❄️@Shadow_support0o❄️