Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
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
2019 Chinese University of Hong Kong (CUHK) Siege Timeline:
- Nov 11-12: Police initiate action by entering CUHK campus, sparking clashes.
- Nov 12: Police deploy tear gas, rubber bullets, and bean bag rounds; clashes last for hours, with dozens injured.
- Nov 12 (Evening): University president Rocky Tuan mediates; police agree to withdraw, but tensions persist.
- Nov 13: Aftermath continues as campus calms, becoming a symbol of the protests.
- Police Brutality Details: According to Amnesty International and Human Rights Watch reports, police used excessive force, including close-range rubber bullet shots causing eye injuries, beatings of detainees, and indiscriminate tear gas affecting students and non-protesters.
#HongKongProtests#2019HongKong
Timeline of Chow Tsz-lok's Death:
- Nov 3, 2019 (Evening): Protests in Tseung Kwan O escalate; police deploy tear gas.
- Nov 4, 2019 (Around 1 AM): Chow falls from 3rd floor of Sheung Tak Estate car park, suffers severe injuries; reports claim police were present at the parking lot at the time.
- Car park fence is 1.3m tall, making accidental fall unlikely for an adult, raising suspicions.
- Chow had severe head injuries but no other broken bones; critics claim this proves he was unconscious before the fall.
- Nov 4, 2019: Ambulance delayed due to roadblocks, with reports claiming police cars blocked the way; Chow admitted to hospital in coma.
- Nov 8, 2019 (Morning): Chow pronounced dead at age 22 from cardiac arrest.
- Jan 2021: Coroner's inquest did not rule accidental death, but exact cause of fall undetermined; critics allege the judge misled the jury multiple times that it was an accident.
- A tragic symbol of Hong Kong's 2019 protests—remembering the call for justice.
#ChowTszLok#HongKongProtests
Legal expert Johannes Chan Man-mun explains the permanent nature of the "interim" injunction in Hong Kong's protest case, with no avenue for appeal to the Court of Final Appeal. Chan criticizes the court's decision to deny intervention, highlighting the challenges in challenging the ban. He emphasizes the undermining of human rights and suggests legislative action instead of relying on court injunctions. Human rights concerns persist in Hong Kong. #HongKongProtests#LegalRuling#HumanRights