Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
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
🚗💨 Introducing the track-ready Cadillac CT5-V! 🎉
This powerful beast now features a Precision Package with enhanced suspension, including increased stiffness and reprogrammed adaptive dampers for an exhilarating ride on the track! 🏁✨
🔧 Key Highlights:
- Carbon-Ceramic Brakes for superior stopping power 🛑
- 6.2L V8 Engine producing an impressive 677 hp and 892 Nm of torque 💪🔥
- RWD for that ultimate driving experience!
Get ready to unleash the power! 💥
#CadillacCT5V#PrecisionPackage#TrackReady#V8Power#Auto
🚗✨Lexus Unveils Upgraded LC Coupe and Convertible!✨🚗
Since its debut in 2017, the Lexus LC family has received a sleek exterior makeover and new interior materials. But that's not all! The updated models now feature a stiffer chassis, thanks to additional braces installed under the floor near the front and rear axles, enhancing overall handling. 🏎️💨
Under the hood, the Lexus LC offers a powerful lineup: a 5.0L V8 engine producing 477 hp or a hybrid V6 powertrain with a 3.5L engine delivering 359 hp. 🔥🔋
#LexusLC#LuxuryCars#CarEnthusiast#DrivingExperience#AutoNews#V8Power#Auto
🌟 Exciting News for Supercar Enthusiasts! 🚗💨
The British have officially kicked off production of the stunning Ginetta Akula! 🦈✨
First unveiled in 2019, this masterpiece is limited to just 20 units, starting at £275,000. 💰
Featuring a classic design, the Akula boasts a powerful 6.4L V8 engine with 600 hp, paired with a manual transmission and rear-wheel drive. 🔥 Despite its robust engine, it weighs only 1.2 tons, accelerating from 0 to 100 km/h in just 2.9 seconds! ⚡️🏁 With a top speed of 290 km/h, this beauty is ready to roar!
Are you ready to experience the thrill? 😍
#GinettaAkula#Supercar#LimitedEdition#V8Power#CarEnthusiast#LuxuryCars#SpeedDemon#Auto
🚗💨 Introducing the BMW M5 Touring! 💨🚗
BMW has officially unveiled the stunning M5 in Touring body style!🌟
Under the hood, it boasts a powerful V8 twin-turbo engine with an impressive total output of 727 horsepower!🔥💪
With the rear seats folded down, you can fit a whopping 1.6 cubic meters of cargo! 🧳📦 And it can sprint from 0 to 100 km/h in just 3.6 seconds!⏱️⚡️
Weighing in at 2508 kg, this beauty is slightly heavier than its sedan counterpart. 🚙✨ This is only the third generation of the M5 in this body style and the first to hit the US market! 🇺🇸
Prices start at $121,500.💵
Get ready for an exhilarating ride! 🏁
#BMW#M5Touring#SportsCar#V8Power#LuxuryPerformance#CarEnthusiast#FastCars#Auto#BMWM🚘🌍
🚗💨 Dodge Unveils the "Farewell" Durango SRT Hellcat Silver Bullet! 💨🚗
This stunning special edition is one of the last to feature the powerful 6.2-liter HEMI V8 engine. As we bid farewell to these muscle machines, the Durango will soon lose its mighty motors. 😢💔
The Silver Bullet edition stands out with its exclusive decor and rich standard features, starting at $113,325**—about **$11,000 more than the previous year's base Durango Hellcat! 💰✨
Get ready to say goodbye to an icon!
#Dodge#DurangoSRT#Hellcat#SilverBullet#MuscleCar#Auto#V8Power#SUV🏁🔥
🚗💨 Introducing the Naran Hypercar: The Ultimate Powerhouse! 🌟
Get ready for the unveiling of the Naran Hypercar in the UK this August! 🇬🇧 With a staggering 1000 horsepower, this beast promises to be the fastest four-seater car in the world, powered by a traditional internal combustion engine—no electric enhancements! ⚡️❌
Only 39 units will be produced, making it an exclusive masterpiece! 🎨✨
Under the hood, it features a 5.0-liter twin-turbo V8, derived from BMW's impressive 4.4-liter engine. 🏎️💥 And yes, it comes with full-wheel drive for that ultimate driving experience! 🌍🚀
#NaranHypercar#CarEnthusiasts#AutomotiveExcellence#V8Power#Hypercar#FastestCar#LuxuryCars#CarLovers#InnovativeEngineering
🚗💨 Introducing the Ford F-150 FP700S! 🎉
Get ready for the ultimate asphalt beast! 🔥 With a powerful 700-horsepower V8 engine, this custom pickup is designed for performance and style.
🔧 Key Features:
• Supercharged 5.0L V8 with Whipple blower 💥
• Sporty exhaust system for that perfect roar 🎶
• Lowered suspension with new shocks for enhanced handling 🏎️
• Stunning 22-inch wheels for a bold look 🛞
• Aerodynamic body elements to slice through the air ✈️
#FordF150#FP700S#CustomGarage#PerformanceTruck#V8Power#AsphaltBeast#FordTuning#Auto
🚗✨ Exciting News from Mercedes-Maybach! ✨🚗
Get ready for the debut of the stunning new convertible on August 15th at the prestigious Pebble Beach! 🌊🏖️
The highly anticipated model has been teased since 2022, and we can't wait to see it in all its glory! 🤩
🔍 Fuel Efficiency: The Mercedes-Maybach SL boasts an impressive fuel consumption of 13.7 liters per 100 km*, closely matching the non-hybrid Mercedes-AMG SL 63 with its powerful 4.0L V8 engine, delivering a thrilling 585 hp and 800 Nm* of torque! 💨🔥
#MercedesMaybach#Convertible#LuxuryCars#PebbleBeach#CarDebut#FuelEfficiency#AMG#V8Power#Auto
🚗✨ Introducing the all-new AC Cobra GT Coupe! 🌟
For the first time in history, this iconic sports car is getting a hardtop version, and it's set to be produced in series! 🏁💨
With stunning carbon body panels, the Coupe is not only beautiful but also powerful! 💪🔥 Choose between two roaring V8 engines: 565 or 730 horsepower!
Starting at just £325,000, this masterpiece is ready to hit the roads! 💷🚀
#ACCobra#SportsCar#CarEnthusiast#LuxuryAutomobile#V8Power#CobraGT#CarLovers#IconicDesign#AutomotiveExcellence
🚗💨Introducing the Doughboy: A Mind-Blowing Restomod with 2838 HP!💥
At the SEMA Show, we unveiled a beast that redefines power and performance! 🔥 This incredible V8 engine boasts a staggering 8.8 liters of displacement and is equipped with dual turbochargers, pushing the limits of automotive engineering. 💪✨
🔧 With the engine strategically shifted 40 cm back towards the center of the wheelbase, every detail has been meticulously crafted from scratch, including the intake, exhaust, and cooling systems. 🛠️
💎 Custom parts were expertly created using CNC machining and 3D printing technology, showcasing innovation at its finest.
Get ready to experience the future of restomods! 🚀
#Doughboy#SEMA2023#Restomod#V8Power#Turbocharged#CarEnthusiast#AutomotiveInnovation#CustomCars#CNC#Auto