Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
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
#ALPHA/USDT analysis :
#ALPHA is currently in an uptrend, forming higher highs (HHs) and higher lows (HLs). The price has retraced to a support zone, and a bounce back is anticipated, with expectations to test previous highs.
TF : 1D
Entry : $0.0870
Target : $0.1340
SL : $0.0674
#ALPHA/USDT analysis :
#ALPHA is presently in a downtrend, trading below the 200 EMA. The price is establishing a pattern of lower lows and lower highs. Currently, it encounters resistance near the 200 EMA, indicating a possible continuation of its bearish trend and a retest of previous lows.
TF : 4h
Entry : $0.0558
Target : $0.0520
SL : $0.0584
Custom Signal: Combination of
👉 Double Bottom
👉 Bollinger Breakout
👉 Trend Line Breakout
We expect the price goes up after double bottom but we can’t trade by using only double bottom pattern. We look for another bullish moves like bollinger or trend line breakout.
What if double bottom, Trend Line Breakout and Bollinger Breakout occur for any asset and it’s price hasn’t risen yet
Bingo !!
#ALPHA did 15% in just 3 hours 👌
Just check the chart, let’s create custom signal