Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
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
#ARDR/USDT analysis :
#ARDR is in a bearish trend, consistently making lower lows (LLs) and lower highs (LHs) below the 200 EMA. The price is currently rejecting from the 200 EMA and is anticipated to resume its downward movement, targeting lower levels.
TF : 4H
Entry : $0.07035
Target : $0.05818
SL : $0.07815
#ARDR👈
https://www.binance.com/en/trade/ARDR_BTC
Buying Zone 540-550👈
Buy in parts n successive dips
Strictly follow as directed
On the verge of hardcore pump
Sell
🤑 620-650
🤑 650-700
🚀 700-800
💃 800-900 & above
#ARDR/USDT analysis :
After breaking out above the 200 EMA, the #Ardor has retested it and consolidated sideways over the support zone. Recently, the price has bounced back from the support zone, and it is anticipated to continue its bullish momentum to test the previous swing high level.
TF : 1D
Entry : $0.09132
Target : $0.12550
SL : $0.07356
Roaring Kitty Hints at Comeback
🐦 Roaring Kitty, a key figure in the GameStop stock surge, teases a return. His recent tweet features a video, sparking nostalgia and speculation about upcoming moves. Kitty symbolizes retail investor resistance against Wall Street, igniting the meme stock revolution in 2021.
🔗Roaring Kitty's tweet
🔗 Whale Alert: $102M in #BTC transferred Details here
🔗 Bithumb launches new ARDR & QKC trading Read more
#GameStop#RoaringKitty#memeStocks#BTC#Crypto#VC#RetailInvestors#WhaleAlert#SouthKorea#Trading#Bithumb#ARDR#QKC#Investing#MarketNews#Finance#Nostalgia#Tease#Speculation