Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
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
The Tuskegee Study was a government experiment from 1932 to 1972. It involved Black men with syphilis who were told they were getting free health care. But doctors did not give them medicine to cure the disease. They just watched the sickness get worse. The men did not know the truth and were not allowed to leave the study. This caused a lot of harm and suffering.
[Read more]
@googlefactss🏥⚠️#TuskegeeStudy#MedicalEthics#History#HealthRights
HeLa cells – taken from Henrietta Lacks in 1951 without her consent – became the first immortal human cell line.
They helped develop the polio vaccine, cancer treatments, and gene mapping, revolutionizing medicine. 7
Her story highlights major ethical questions in science and patient rights.
[Read more]
@googlefactss
#HeLa#MedicalEthics#ScienceHistory#HenriettaLacks
Active euthanasia is when a doctor directly ends a patient’s life, like using a lethal injection. Passive euthanasia is when doctors stop treatment or life support, letting the person die naturally.
Passive euthanasia is legal in many countries. Active euthanasia is only legal in a few, like the Netherlands, Belgium, Luxembourg, Canada, Spain, Portugal, Colombia, Ecuador, and New Zealand. Some U.S. states allow assisted dying, but not active euthanasia.
Many people find passive euthanasia more acceptable. Active euthanasia is more controversial. People worry about abuse or moral problems. Support often depends on whether a person is terminally ill or in unbearable suffering.
⚕️🛌💉🌍📜
[Check our poll]
[Read some more]
@googlefactss
#Euthanasia#PassiveVsActive#MedicalEthics#RightToDie#HealthEducation#TeenLearning#GlobalLaw