设置 FastApi 单进程线程数 RunVar(“_default_thread_limiter”).set(CapacityLimiter(200))

from fastapi import FastAPI
import time
import asyncio
import os
#import concurrent.futures
from anyio.lowlevel import RunVar
from anyio import CapacityLimiter
RunVar(“_default_thread_limiter”).set(CapacityLimiter(200))
app = FastAPI(docs_url=None)
@app.get(“/async_sleep”)
async def async_sleep():
time.sleep(4)
return {“message”: “sync sleep”}
@app.get(“/sleep”)
def sleep():
time.sleep(4)
return {“message”: “sync sleep”}
@app.get(“/echo”)
def echo():
st = time.time()
try:
return {“message”:”OK”}
except Exception as e:
pass
finally:
st2 = time.time() – st
print(f”Log info {st2}”)