Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
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
Our #LassaFever situation report for week 1, 2025 has been published.
54 cases have been confirmed and 10 deaths have been recorded across 6 states in Nigeria.
🔗Download, read, and share via https://ncdc.gov.ng/ncdc.gov.ng/themes/common/files/sitreps/1bded1685f8dc4961a169a274be138c2.pdf
Our #LassaFever situation reports for weeks 42, 2024 have been published.
The weekly report provides a summary of the #LassaFever epidemiological situation and response activities in Nigeria.
🔗Download, read, and share via
https://ncdc.gov.ng/diseases/sitreps/?cat=5&name=An%20update%20of%20Lassa%20fever%20outbreak%20in%20Nigeria
Our #LassaFever situation report for week 40, 2024 has been published.
1025 cases have been confirmed and 174 deaths have been recorded across 28 states in Nigeria.
🔗Download, read, and share via
https://ncdc.gov.ng/diseases/sitreps/?cat=5&name=An%20update%20of%20Lassa%20fever%20outbreak%20in%20Nigeria
Our latest #LassaFever situation report shows that 982 confirmed cases and 168 deaths have been recorded in 2024.
This loss of lives is not just a number but a significant loss of a family member, friend, colleague or a seasoned healthcare worker
CEPI vaccines writes on the critical need for a #LassaFever vaccine to control this disease.
🔗
https://cepi.net/lassa-vaccine-urgently-needed-save-thousands-lives
Situation report: https://ncdc.gov.ng/diseases/sitreps/?cat=5&name=An%20update%20of%20Lassa%20fever%20outbreak%20in%20Nigeria
We have published our latest #LassaFever epidemiology data.
Here you can find a summary of all the latest figures in our weekly infographic.🔽
The full report can be accessed via
🔗https://ncdc.gov.ng/diseases/sitreps/?cat=5&name=An%20update%20of%20Lassa%20fever%20outbreak%20in%20Nigeria
Our #LassaFever situation report for week 32, 2024 has been published.
The weekly report provides a summary of the #LassaFever epidemiological situation and response activities in Nigeria.
🔗Download, read, and share via
https://ncdc.gov.ng/diseases/sitreps/?cat=5&name=An%20update%20of%20Lassa%20fever%20outbreak%20in%20Nigeria
Our #LassaFever situation report for week 30, 2024 has been published.
The weekly report provides a summary of the #LassaFever epidemiological situation and response activities in Nigeria.
🔗Download, read, and share via
https://ncdc.gov.ng/diseases/sitreps/?cat=5&name=An%20update%20of%20Lassa%20fever%20outbreak%20in%20Nigeria
Our #LassaFever situation reports for weeks 28 and 29, 2024 have been published.
The weekly report provides a summary of the #LassaFever epidemiological situation and response activities in Nigeria.
🔗Download, read, and share via
https://ncdc.gov.ng/diseases/sitreps/?cat=5&name=An%20update%20of%20Lassa%20fever%20outbreak%20in%20Nigeria
Our #LassaFever situation report for week 25, 2024 has been published.
The weekly report provides a summary of the #LassaFever epidemiological situation and response activities in Nigeria.
🔗Download, read, and share via
https://ncdc.gov.ng/diseases/sitreps/?cat=5&name=An%20update%20of%20Lassa%20fever%20outbreak%20in%20Nigeria
Our #LassaFever situation report for week 24, 2024 has been published.
The weekly report provides a summary of the #LassaFever epidemiological situation and response activities in Nigeria.
🔗Download, read, and share via
https://ncdc.gov.ng/diseases/sitreps/?cat=5&name=An%20update%20of%20Lassa%20fever%20outbreak%20in%20Nigeria
Our #LassaFever situation report for week 23, 2024 has been published.
The weekly report provides a summary of the #LassaFever epidemiological situation and response activities in Nigeria.
🔗Download, read, and share via
https://ncdc.gov.ng/diseases/sitreps/?cat=5&name=An%20update%20of%20Lassa%20fever%20outbreak%20in%20Nigeria
Our #LassaFever situation report for week 21, 2024 has been published.
The weekly report provides a summary of the #LassaFever epidemiological situation and response activities in Nigeria.
🔗Download, read, and share via
https://ncdc.gov.ng/diseases/sitreps/?cat=5&name=An%20update%20of%20Lassa%20fever%20outbreak%20in%20Nigeria