Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
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
#Re#Timeline
模块:nep.timeline.re_telegram
简介:Re Telegram
版本:170-Akiyama
更新时间:2024/12/09 15:29:52
更新日志:
Now you can delete your messages normally without having to delete them multiple times
@lsposed_Modules_Updates_Trackers | @lsposed_Geeks_Bot
📄Timeline, a place where you can see all the messages from all subscribed channels.
❤️Redesigned In version 11.10.0 on June 20, 2024.
🗓Was added in version 5.0 on August 19, 2016.
#features#message#timeline#feed
#graph_messenger#telegram
@GraphMessenger
@GraphMessengerTips
#go#archival#data_archiving#data_import#family_history#self_hosted#timeline
Timelinize helps you organize your personal data from different sources like photos, messages, and social media into a single timeline on your computer. This keeps your data private and under your control, unlike cloud services. You can import data from many places, view it on a map, and see conversations across different platforms. It's like having a personal journal that you can add to and keep forever, without relying on companies to store it for you. This way, you can keep your memories safe and easily look back at them whenever you want.
https://github.com/timelinize/timelinize
After this reset, you will begin the New Earth timeline with new belief systems will guide you. But that is in the future.
For today, create boundaries with both the past and the future so you can simply be in this here and now moment.
The past no longer defines you. The future is what you decide upon.
Yah bless 🌹 Love Carolyn
✨ Get your FREE Quantum Energy Balancing & Tune-up https://www.144godcode.com/freebie
#timeline#newearth#reset#rebirth#therealyou #144000 #144GODCODE#APTTMH