Skip to content

Commit

Permalink
Implement async lifespan for FastAPI and refactor HTTP client usage i…
Browse files Browse the repository at this point in the history
…n surface query service
  • Loading branch information
sigurdp committed Nov 22, 2024
1 parent 0eb2eb5 commit 5e4f0b9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
23 changes: 23 additions & 0 deletions backend_py/primary/primary/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,30 @@ def custom_generate_unique_id(route: APIRoute) -> str:
return f"{route.name}"





import httpx
from contextlib import asynccontextmanager

@asynccontextmanager
async def app_lifespan(app: FastAPI):
print("start lifespan")
#limits = httpx.Limits(max_keepalive_connections=200, max_connections=300)

#app.state.requests_client = httpx.AsyncClient(http2=True, verify=False, limits=limits)
#app.state.requests_client = httpx.AsyncClient(verify=False)
app.state.requests_client = httpx.AsyncClient()
yield

await app.state.requests_client.aclose()
print("end lifespan")




app = FastAPI(
lifespan=app_lifespan,
generate_unique_id_function=custom_generate_unique_id,
root_path="/api",
default_response_class=ORJSONResponse,
Expand Down
6 changes: 5 additions & 1 deletion backend_py/primary/primary/routers/surface/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
from typing import Annotated, List, Optional, Literal

from fastapi import APIRouter, Depends, HTTPException, Query, Response, Body, status
from fastapi import APIRouter, Depends, HTTPException, Query, Response, Request, Body, status
from webviz_pkg.core_utils.perf_metrics import PerfMetrics

from primary.services.sumo_access.case_inspector import CaseInspector
Expand Down Expand Up @@ -231,6 +231,7 @@ async def post_get_surface_intersection(

@router.post("/sample_surface_in_points")
async def post_sample_surface_in_points(
request: Request,
case_uuid: str = Query(description="Sumo case uuid"),
ensemble_name: str = Query(description="Ensemble name"),
surface_name: str = Query(description="Surface name"),
Expand All @@ -242,7 +243,10 @@ async def post_sample_surface_in_points(

sumo_access_token = authenticated_user.get_sumo_access_token()

async_client = request.app.state.requests_client

result_arr: List[RealizationSampleResult] = await batch_sample_surface_in_points_async(
async_client=async_client,
sumo_access_token=sumo_access_token,
case_uuid=case_uuid,
iteration_name=ensemble_name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class _PointSamplingResponseBody(BaseModel):


async def batch_sample_surface_in_points_async(
async_client: httpx.AsyncClient,
sumo_access_token: str,
case_uuid: str,
iteration_name: str,
Expand Down Expand Up @@ -117,14 +118,19 @@ async def batch_sample_surface_in_points_async(

json_request_body = request_body.model_dump()

async with httpx.AsyncClient(timeout=300) as client:
LOGGER.info(f"Running async go point sampling for surface: {surface_name}")
LOGGER.info(f"Running async go point sampling for surface: {surface_name}")
perf_metrics.record_lap("prepare_call")

perf_metrics.record_lap("prepare_call")

response: httpx.Response = await client.post(
url=SERVICE_ENDPOINT, json=json_request_body
)
response: httpx.Response = await async_client.post(url=SERVICE_ENDPOINT, json=json_request_body)


# async with httpx.AsyncClient(timeout=300) as client:
# LOGGER.info(f"Running async go point sampling for surface: {surface_name}")

# perf_metrics.record_lap("prepare_call")

# response: httpx.Response = await client.post(url=SERVICE_ENDPOINT, json=json_request_body)

perf_metrics.record_lap("main-call")

Expand Down

0 comments on commit 5e4f0b9

Please sign in to comment.