Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
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
🇮🇷 Breaking: University protests flare up across Iran
New student protests erupted at major Iranian universities as the new term began, with demonstrators honouring victims of last month’s deadly crackdown. Clashes were reported in Tehran, with chants targeting Supreme Leader Ali Khamenei and reviving the “Woman, Life, Freedom” movement.
The unrest comes amid rising geopolitical tensions, as the U.S. boosts its regional military presence while nuclear talks with Tehran remain unresolved. #News#Iran#University
「💻」#university#studying_tips
new semester, better me 📚🦪.
Yesterday I did a lot of stuff including unpacking and setting up my room for the semester.
Of course I will redecorate it, however I shouldn’t forget that this room is for only one semester
✨
Inshallah will go to study away
😻
~🐈~
↬@jastm1🌊
「💻」#university#studying_tips
❤️Some college x School inspo
✨A new chapter is here!✨
Are you ready for school/college? ✨
This is the perfect time to set new ambitions and new goals. A chance to start fresh, grow, and chase the best version of yourself. 🌱
Don’t be shy, step forward with confidence, give it your best, and make this year unforgettable.💗🫶
~🐈~
↬@jastm1🌊
「💻」#university#studying_tips
Checklist of things to pack for UNIVERSITY🥳!
Most of you might be overwhelmed with the amount of things to do, but don't worry you've got this 💪.
Save not to loose!
~🐈~
↬@jastm1🌊
https://www.coursera.org/specializations/data-science-python?utm_medium=email&utm_source=marketing&utm_campaign=2QBGEJZREeajEnVsu251ig
The 5 courses in this #University of #Michigan specialization introduce learners to data science through the python programming language. This skills-based specialization is intended for learners who have basic a python or programming background, and want to apply statistical, machine learning, information visualization, and text analysis techniques to gain new insight into their data.
Introduction to Data Science with Python (course 1), Applied Plotting, Charting & Data Representation in Python (course 2), and Applied Machine Learning in Python (course 3) should be taken in order and prior to any other course in the specialization. After completing those, courses 4 and 5 can be taken in any order. All 5 are required to earn a certificate.
Bizning faxriy oqituvchimiz Sultanov Ruslan Rustamovich
➖➖➖➖
Наш почётный учитель Султанов Руслан Рустамович
#tiue#university#университет
Telegram | Instagram | Website
Congratulations Class of 2027! Welcome to New Uzbekistan University! 🎉
#NewUU2027#NewUzbekistanUniversity#University
Check out the full video on our YouTube!
Best Regards,
NewUU Student Council
Telegarm | Instagram
🇰🇷Janubiy Koreya ta’lim tashkilotlari bilan hamkorlik aloqalari mustahkamlanmoqda
🇺🇿Batafsil
___
Strengthening cooperation with South Korean educational organizations
🇬🇧Eng
___
Укрепление сотрудничества с Южно-Корейскими образовательными организациями
🇷🇺Ru
#GraduateSchool#Soonchunhyang#University#Cooperation
Web-site | Telegram | Facebook | Instagram
Handong Global universiteti bilan hamkorlikni yangi bosqichlari
🇺🇿Batafsil
___
New Stages of Collaboration with Handong Global University
🇬🇧Eng
___
Новые этапы сотрудничества с Глобальным университетом Хандонг
🇷🇺Ru
#GraduateSchool#Handong#Collaboration#University
Web-site | Telegram | Facebook | Instagram