Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
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 : "The Mysterious Nightmare"
The nightmare I got a day before,
Was full of science and plenty more,
The world of progress that we think of tonight,
The robots were working, it was a dreadful night.
It was when the human were servants,
Robots were having bungalows or villas,
We were that time the only vagrants.
The humans were robots and the robots were humans,
They all were having parties and we were being summoned.
They were instructing us as if we were dolls,
But the reality was that it was they robotic dolls.
They were the ones we then pray to God,
The one who takes our ease and comfort and many more.
Suddenly, with a jerk of dream, I saw someone with spiritual rays,
I was unable to see the face but was feeling the sense of gaze.
The voice says," Oh my dear child, calm your mind and enjoy the note of nature".
I then opened my eyes to the end of this dreamy adventure.
In the morning dawn,
With the closet of bizarre dreams and an upset mind,
I woke up on the verge of a melancholic night.
Suddenly, I heard the crazy cries of birds,
It was so blissful that I can't express it within a few words.
Enjoying the subtlety of nature,
I realised the existence of each creature.
Praying to God I would never have such dream,
I started my routine work with an energising scream.
#review
#Cookie
#Nightmare
"Goodnight" and then I sleep. I spent the day with you and I plan on continuing so I keep you in my dreams. It feels magical. Almost surreal. This is what I've longed for. Even after you broke me, you never left my mind. Except this time you were a nightmare, not a dream
#review#nightmare#her#broken
Mi sono stancato di influencer e tiktoker vari, buoni solo a pietrificare l'attenzione verso cose futili.
#vaporonda#vaporwave#vapourwave#vaporart#seapunk#webpunk#glitch#glitchart#glitchartistscollective#chillwave#aesthetic#vaporwaveaesthetic#vaporwaveaesthetics#malinconia#melancholy#tristezza#sadness#sadboy#lofi#apparenza#appearance#appearance#illusione#illusion#incubo#nightmare