Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
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
#RIF/USDT analysis :
Following a significant decline of -83% from its all-time high, the price has found support at the $0.0600 level. Subsequently, it has bounced back and successfully broken above the trendline, indicating a confirmation of anticipated bullish momentum. It is expected that the price will continue this upward trajectory and test the previous swing high.
TF : 1W
Entry : $0.1117
Target : $0.3377
SL : $0.0766
#RIF/USDT analysis :
#RIF is trading above the support zone and the 200 EMA. The price is anticipated to rebound from this level and proceed to rise, testing the previous swing high.
TF : 15min
Entry : $0.0977
Target : $0.1007
SL : $0.0956
#RIF yielded an impressive 3,010% return in a few days ⭐
If you used 200x leverage on this signal, you would've made a mind-blowing profit of 12,040%🤩
$50💰 would return $750💵
$50 💰 would return $1,500
💲
$50💰 would return $6,000💰
When you understand the market like we do, every move becomes predictable With 5+ years of trading experience, we spot these moves before they happen 💯
If you want daily signals like this, DM me to join our private group! 👇
@Kevindexter✅
#RIF
I'm expecting roughly this kind of movement on this asset, either with streaming start ups behind our magnets from above.
📈Sign up on Bybit to trade with me