Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
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
🔥NEW RELEASE🔥
😁Dynamically Combining Inside & Outside Passing by Jason Rau😁
➡️Unlock a truly adaptive guard passing game with Jason Rau’s groundbreaking system that seamlessly blends tight inside pressure passing with loose outside movement.
➡️Learn how to fluidly switch between styles depending on your opponent’s reactions, making your passing game feel unpredictable, overwhelming, and unshakable.
$127😍$11.99😍
❗️Channel Info and FAQ (MUST CHECK BEFORE BUY)❗️
📱Payment: 📱📱📱
💰Submeta FOR FREE
🌈UFC Picks
👑MasterClass
✈️@bjjfanaticsinstructionals
#JasonRau#Passing#NoGi#ADCC
💁♀️Buy
💁♀️
🔥NEW RELEASE🔥
😁Understanding The Reap By Jason Rau😁
➡️Unlock the power of one of Brazilian Jiu-Jitsu’s most misunderstood and effective positions with Understanding The Reap by Jason Rau.
➡️This incredibly comprehensive course takes you deep into the intricacies of the "reap" — a position that is often overlooked, yet crucial for mastering lower body submissions, sweeps, guard passes, and even back takes.
➡️Whether you’re a beginner or an advanced practitioner, Jason Rau’s detailed breakdown of the reap will give you the tools to control and submit your opponent, defend with confidence, and add an entirely new dimension to your BJJ game.
$99😍$9.99😍
❗️Channel Info and FAQ (MUST CHECK BEFORE BUY)❗️
📱Payment: 📱📱📱
💰Submeta FOR FREE
🌈UFC Picks
👑MasterClass
✈️@bjjfanaticsinstructionals
#JasonRau#BJJFanatics#NoGi#Leglocks
💁♀️Buy
💁♀️
🔥NEW RELEASE🔥
😁Outside Ashi Reloaded by Jason Rau😁
➡️Take your leg lock game to the next level with Outside Ashi Reloaded, an innovative instructional series by Jason Rau — a black belt under Matt Serra and one of our best-selling instructors.
➡️Jason dives deep into the strategic interplay between these attacks, teaching you how to create relentless pressure and chain submissions so that when your opponent defends one, they inevitably expose themselves to the other.
$127😍$11.99😍
❗️Channel Info and FAQ (MUST CHECK BEFORE BUY)❗️
📱Payment: 📱📱📱
💰Submeta FOR FREE
🌈UFC Picks
👑MasterClass
✈️@bjjfanaticsinstructionals
#JasonRau#Leglocks#NoGi#OutsideAshi
💁♀️Buy
💁♀️
🔥NEW RELEASE🔥
😁Dynamically Taking The Back (Turtle) by Jason Rau😁
➡️Unlock the Ultimate Back-Taking Blueprint from Turtle with Dynamically Taking the Back (Turtle) — the revolutionary course by elite BJJ black belt Jason Rao, designed to take your control, timing, and back-attack game to a whole new level.
➡️From gripping strategies and handfighting to dynamic movement and pressure mechanics, you’ll learn how to turn every turtle situation into a back take with confidence and precision.
$99😍$9.99😍
❗️Channel Info and FAQ (MUST CHECK BEFORE BUY)❗️
📱Payment: 📱📱📱
💰Submeta FOR FREE
👑MasterClass
✈️@bjjfanaticsinstructionals
#JasonRau#Turtle#NoGi#ADCC
💁♀️Buy
💁♀️