Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
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
🚀 Big Win Episode 4! Our games smash into 2025 with massive wins!
This year's first episode of big wins brought three incredible prizes:
🎸Dr. Rock & the Riff Reactor — Super bonus bet $3000, win $102,918!
🎅 Gates of Santa — Bet 20 RUB, win 300,000 RUB ($3,000)!
⚡️ Dr. Rock & the Riff Reactor — Bet 3600 RUB, win 600,000 RUB ($6,000)!
🕹 Ready to make history with your spins? Let's go! 🎯
#TrueLabGames#BigWins
🎅 Big Wins in our new slot Gates of Santa already here🎄
The magic of the holiday season is real, and Gates of Santa proves it with some jaw-dropping wins! Take a look:
😮A lucky player turned 1 GBP into an incredible 10,337.7 GBP! That’s a multiplier of x10,337.7!
🦄With just a 1.25 EUR bonus bet, one player hit a jackpot of 12,241.4 EUR, an astonishing x12,241.4 multiplier!
💥A high-stakes bet of 2 EUR brought home an epic 21,600 EUR! That’s a holiday-perfect x10,800 multiplier! 🤑
🎄 The New Year and Christmas have already brought joy and fortune to hundreds of players in Gates of Santa. Will you be next? Try your luck today!
#BigWins#GatesOfSanta#HolidayMagic
In Battle Rage, the Rage multiplier kicks into action when the Emperor Wild lands, activating the Rage accumulator and boosting your potential for huge wins up to x100,000! The Rage Multiplier has no limits and carries over into Free Spins for even more intense payouts.
Prepare to harness the power of Rage and dominate the arena when Battle Rage launches on October 15, 2024!
#BattleRage#RageMultiplier#SlotGames#BigWins#TrueGames#TrueSlots#TrueLab