Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
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
🌎 Ancient lake beds called "playa lakes" preserve natural records of Earth's past. Layers of sediment in these basins capture pollen, plant remains, and even insect fossils, revealing climate cycles and environmental shifts over thousands of years. Some dry lakebeds in the U.S. Southwest show records going back 120,000 years. ✨
#geology⚡#paleoclimate⚡#lakes
👉subscribe Interesting Planet
👉more Channels
🌍 Canada has more lakes than any other country, and many are so large they contain whole islands with their own lakes—making some islands-in-a-lake-on-an-island truly unique on Earth. ✨
#islands⚡#archipelago⚡#lakes⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography🌍
🌍 Africa’s Lake Victoria is the largest tropical lake in the world. Its outflow forms the start of the Nile River, which journeys over 6,600 kilometers north to the Mediterranean Sea. ✨
#lakes⚡#rivers⚡#Africa⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌎 The salt-loving halophiles of California’s pink Lake Hillier thrive where few others survive. These tiny microbes give the lake its vivid color by producing pigments that protect them from extreme salt and sunlight—turning the water a bubblegum pink! ✨
#microbes⚡#pigment⚡#lakes
👉subscribe Interesting Planet
🌍 Russia’s Lake Karachay is so contaminated from past nuclear waste that standing on its shore for just an hour could be fatal—one of the most polluted lakes on the planet. ✨
#rivers⚡#lakes⚡#pollution⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 Some lake water can take hundreds of years to fully circulate from the surface to the deepest layers and back again, creating slow, hidden water cycles that influence local climates and ecosystems. ✨
#hydrology⚡#lakes⚡#cycles⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 Some of the world’s oldest lakes, like Lake Ohrid in Europe, have existed for more than one million years, preserving unique species found nowhere else on Earth. ✨
#lakes⚡#biodiversity⚡#longevity⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 Siberia’s Lake Baikal freezes in winter, forming ice so clear that cracks and bubbles can be seen meters deep. In spring, melting ice creates musical sounds as it shifts and breaks apart. ✨
#lakes⚡#rivers⚡#Siberia⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 Lake Titicaca, perched almost 3,812 meters above sea level, is the world’s highest navigable lake by large boats. Its clear waters straddle the border between Peru and Bolivia in the Andes. ✨
#lakes⚡#Andes⚡#altitude⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography🌍
🌍 In Africa’s Lake Natron, minerals in the water turn it bright red during the dry season. The lake’s extreme chemistry preserves animal remains, creating eerie natural “statues.” ✨
#lakes⚡#Africa⚡#chemistry⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 The world’s clearest lake is Blue Lake in New Zealand. Its pure water is so transparent that objects can be seen up to 80 meters below the surface—almost like looking through glass. ✨
#lakes⚡#water⚡#clarity⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 Finland boasts about 188,000 lakes, earning it the nickname "Land of a Thousand Lakes." Many of these lakes were formed by retreating glaciers at the end of the last Ice Age. ✨
#lakes⚡#glaciation⚡#Finland⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels