Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
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
📖Title : Lady Chatterley's Lover
✍️Author : D. H. Lawrence
⭐️Rating : 3.50/5 (Goodreads)
📆Published : Jan 01, 1928
————————————————
Summary:Lady Chatterley’s Lover by D.H. Lawrence tells the story of Constance (Lady Chatterley), a young woman trapped in a passionless marriage to Sir Clifford, a paralyzed war veteran more concerned with intellect than intimacy. Emotionally and physically unfulfilled, she begins an affair with Oliver Mellors, the gamekeeper on their estate. Their relationship blossoms into a deep, sensual, and emotional connection, breaking through rigid class divisions and societal expectations. Through this affair, Constance rediscovers her own desires and the power of human connection. The novel explores themes of love, class, sexuality, and personal freedom in a repressive, post-war English society.
————————————————
#erotic#fiction#romance#classics@Bookslibraryofficial@free_novellas@the_ancient_library@thesciencelabs
#Movie Bad Teacher
⚔️Genres: #Comedy#Erotic 18+
🔊 Language : #🔥English
💿 Quality : BluRay HD
🔖 sʜᴏʀᴛ : She’s broke, she’s reckless, and she’s the kind of teacher who cares more about high heels than homework. When her plan to snag a rich husband crashes and burns, she hatches an even crazier scheme…
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
480p👇👇
https://t.me/+w-JLkZEuxFIyMmQ0
720p👇👇
https://t.me/+w-JLkZEuxFIyMmQ0
📌 Add your channel @freebets89
#Movie Bad Teacher
⚔️Genres: #Comedy#Erotic 18+
🔊 Language : #🔥English
💿 Quality : BluRay HD
🔖 sʜᴏʀᴛ : She’s broke, she’s reckless, and she’s the kind of teacher who cares more about high heels than homework. When her plan to snag a rich husband crashes and burns, she hatches an even crazier scheme…
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
480p👇👇
https://t.me/+w-JLkZEuxFIyMmQ0
720p👇👇
https://t.me/+w-JLkZEuxFIyMmQ0
📌 Add your channel @freebets89
#Movie Bad Teacher
⚔️Genres: #Comedy#Erotic 18+
🔊 Language : #🔥English
💿 Quality : BluRay HD
🔖 sʜᴏʀᴛ : She’s broke, she’s reckless, and she’s the kind of teacher who cares more about high heels than homework. When her plan to snag a rich husband crashes and burns, she hatches an even crazier scheme…
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
480p👇👇
https://t.me/+w-JLkZEuxFIyMmQ0
720p👇👇
https://t.me/+w-JLkZEuxFIyMmQ0
📌 Add your channel @freebets89
#Movie Bad Teacher
⚔️Genres: #Comedy#Erotic 18+
🔊 Language : #🔥English
💿 Quality : BluRay HD
🔖 sʜᴏʀᴛ : She’s broke, she’s reckless, and she’s the kind of teacher who cares more about high heels than homework. When her plan to snag a rich husband crashes and burns, she hatches an even crazier scheme…
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
480p👇👇
https://t.me/+w-JLkZEuxFIyMmQ0
720p👇👇
https://t.me/+w-JLkZEuxFIyMmQ0
📌 Add your channel @freebets89
#Movie Bad Teacher
⚔️Genres: #Comedy#Erotic 18+
🔊 Language : #🔥English
💿 Quality : BluRay HD
🔖 sʜᴏʀᴛ : She’s broke, she’s reckless, and she’s the kind of teacher who cares more about high heels than homework. When her plan to snag a rich husband crashes and burns, she hatches an even crazier scheme…
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
480p👇👇
https://t.me/+w-JLkZEuxFIyMmQ0
720p👇👇
https://t.me/+w-JLkZEuxFIyMmQ0
📌 Add your channel @freebets89
#Movie Bad Teacher
⚔️Genres: #Comedy#Erotic 18+
🔊 Language : #🔥English
💿 Quality : BluRay HD
🔖 sʜᴏʀᴛ : She’s broke, she’s reckless, and she’s the kind of teacher who cares more about high heels than homework. When her plan to snag a rich husband crashes and burns, she hatches an even crazier scheme…
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
480p👇👇
https://t.me/+w-JLkZEuxFIyMmQ0
720p👇👇
https://t.me/+w-JLkZEuxFIyMmQ0
📌 Add your channel @freebets89