Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
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
Lassa fever (LF) initially presents like other common illnesses accompanied by a fever, such as malaria.
Healthcare workers are urged to remain vigilant.
Consider a #LF diagnosis when seeing patients showing symptoms of fever.
🔗Health Advisory
https://www.ncdc.gov.ng/news/507/lassa-fever-public-health-advisory
📜
Annual outbreaks of #LassaFever involve the infection and death of a loved family member, a spouse, a parent, and often seasoned healthcare workers and team members.
Our #LF health advisory provides measures through which health workers and the public can protect themselves.
🔗Read via https://www.ncdc.gov.ng/news/507/lassa-fever-public-health-advisory
📣JOIN IN
Our #LassaFever Research Colloquium is LIVE!
The colloquium aims to review the current situation and identify research priorities that will bring about a better understanding of #LF transmission using a One Health approach
🗓️TODAY, May 2nd, 2023
🕗 9:00 a.m. WAT
Register to join:
https://us06web.zoom.us/webinar/register/WN_N0qf8u_IR0W_8k1UaiCnaA
Healthcare workers should raise suspicion for patients with a history of contact with
➡️a probable/confirmed case of #LassaFever within 21 days of onset of fever
➡️body fluids/tissues of a patient with a febrile illness, symptoms & signs suggestive of #LF
🔗Read and share
https://www.ncdc.gov.ng/themes/common/docs/protocols/92_1547068532.pdf
Healthcare workers are advised to always be vigilant - lookout for symptoms of #LassaFever, not all fevers are malaria.
Any febrile illness that has not responded to the use of anti-malaria or antibiotics after 48 hrs should raise an index of suspicion for #LF.
#LassaFeverInfo