Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
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
#BNX/USDT analysis :
#BNX has experienced a significant drop of approximately 74% from its peak and has found support at a previously respected support zone. The price is currently forming a double bottom reversal pattern at this critical level. It is expected that the price will bounce back from this zone and continue to rise, targeting previous highs.
TF : 1D
Entry : $0.5600
Target : $1.6045
SL : $0.4026
#BNX 30m indicator strategy
👨✈️
GG-Shøt caught the trend at 0.26 - the rest is history /// 😘^^
+4159% max profit (10x lev)
+8318% max profit (20x lev)
The chart speaks for itself.
Others watch
🕺 We execute.
#BNX result
All target achieved in just 6 days ✅
One more huge quick profit 25%🤑💰🤑
👉 Still thinking? The more you wait more you lose profit
☎️ Contact @MichaelStrategiesVip for membership and grab next breakout signal
#BNX result
3 nd target achieved in just 6 house ✅✅✅
One more huge quick profit 18.6%🤑💰🤑
👉 Still thinking? The more you wait more you lose profit
☎️ Contact @MichaelStrategiesVip for membership and grab next breakout signal