Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
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
⭐️#GBPUSD: Weak Market & Bearish Continuation
The charts are full of distraction, disturbance and are a graveyard of fear and greed which shall not cloud our judgement on the current state of affairs in the 📉GBPUSD pair price action which suggests a high likelihood of a coming move down.
📊
🔅GBPUSD Will Move Lower! Short!📉
- - - - - - - -
Take a look at our analysis for 📊GBPUSD.
The market is approaching a significant resistance area 1.352.
Due to the fact that we see a positive bearish reaction from the underlined area, I strongly believe that sellers will manage to push the price all the way down to 1.345 level.
- - - - - - - -
#freesignal#gbpusd
- - - - - - - -
🌐Free Copy Trading: Link
📲Join VIP via Bot:Link
❓Official Contact:@signalprovidercontact
🔅GBPUSD Is Going Up! Long!📈
- - - - - - -
Please, check our technical outlook for 📊GBPUSD.
The market is approaching a key horizontal level 1.349.
Considering the today's price action, probabilities will be high to see a movement to 1.351.
- - - - - - - -
#freesignal#gbpusd
- - - - - - - -
🌐Free Copy Trading: Link
📲Join VIP via Bot:Link
❓Official Contact:@signalprovidercontact
🔅GBPUSD Will Go Down! Short!📉
- - - - - - - -
Please, check our technical outlook for 📊GBPUSD.
The market is testing a major horizontal structure 1.321.
Taking into consideration the structure & trend analysis, I believe that the market will reach 1.315 level soon.
- - - - - - - -
#freesignal#gbpusd
- - - - - - - -
🌐Free Copy Trading: Link
📲Join VIP via Bot:Link
❓Official Contact:@signalprovidercontact