Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
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
Getting excited over the 6.6 sales? Ask yourself if you really need the item before adding it to your cart!
Consider using or repairing what you have, instead of replacing it. #ReduceReuseRecycle
Check out some tips here: https://www.youtube.com/playlist?list=PLPX9rYUbMZF3GLoLkP2Bxd0TelSHylBOu
It can be very tempting to do online shopping this circuit breaker period, but think about whether you really need it before you put it in your cart! #ReduceReuseRecycle
Dressing up as your favourite superhero this Halloween?
Get creative and make a costume using everyday household items. When you’re done, recycle or reuse what you can of your outfit, so that your superhero legacy will continue to live on 🦸♂️
#3Rs#ReduceReuseRecycle
Be a green champion by refusing disposable plastic bags and using a reusable bag. Marvel at the environmental gains you achieve on a daily basis 💪#3Rs#ReduceReuseRecycle
✨ Like training dragons, recycling right takes practice. Master these 3 moves to #RecycleRight: Empty ➡️ Rinse ➡️ Dry
♻️www.nea.gov.sg/3R#3Rs#ReduceReuseRecycle#TowardsZeroWaste#SustainableSG
60 years of progress, but what’s next for Singapore?
Higher temperatures, more wet and dry extremes, and accelerating increase in mean sea levels are projected. In the worst-case scenario, the 𝗺𝗲𝗮𝗻 𝘀𝗲𝗮 𝗹𝗲𝘃𝗲𝗹 around Singapore is projected to 𝗿𝗶𝘀𝗲 𝗯𝘆 𝟭.𝟭𝟱𝗺 𝗯𝘆 𝘁𝗵𝗲 𝗲𝗻𝗱 𝗼𝗳 𝘁𝗵𝗲 𝗰𝗲𝗻𝘁𝘂𝗿𝘆, and up to 𝗮𝗿𝗼𝘂𝗻𝗱 𝟮𝗺 𝗯𝘆 𝟮𝟭𝟱𝟬.
Let’s commit to a sustainable future by taking climate actions to reduce our waste and carbon footprint. #3Rs#ReduceReuseRecycle#SG60
Happy 60th National Day, Singapore! 🇸🇬
As we celebrate our progress as a nation, let’s also commit to building a #SustainableSG for present and future generations. Let’s reduce our carbon footprint by taking climate actions such as adopting energy-saving and #ReduceReuseRecycle habits.
#SG60#KeepSGClean
#ProTip: There’s no need to sort your recyclables! Paper, plastic, glass, and metal can all be placed in the recycling bins ♻️✌️
But do remember to CHECK and CLEAN before you RECYCLE: www.go.gov.sg/recycleright
#RecycleRight#ReduceReuseRecycle
Give your home a refreshed look that brings renewed fortune to every corner.
Revamp your space by rearranging your furniture or upcycling your Lunar New Year decorations.
#3Rs#ReduceReuseRecycle#SustainableSG#TowardsZeroWaste#LunarNewYear2024
Switch off for #EarthHour at 8:30pm!
But let's not stop there. Let this yearly reminder inspire us to continue making sustainable choices. Your everyday choices matter more than you might think!
Small steps such as adopting energy-saving habits and recycling right can make a difference. Imagine these small acts being amplified by millions worldwide. 🌍💚
#EnergyEfficiency#ReduceReuseRecycle#RecycleRight
Building climate resilience will keep Singapore steady pom pi pi for the next 60 years! 💃🕺
Let’s take action for a #SustainableSG by adopting energy-saving and #ReduceReuseRecycle habits.
#SG60#ClimateActionSG
Tidying up your house over the weekend?
Here’s a #SustainabilityHack to keep your table free of messy wires! You can also beautify this DIY cable organiser to match your room design.
#3Rs#ReduceReuseRecycle#SustainableSG