Omkar008 commited on
Commit
bacf700
·
verified ·
1 Parent(s): 3f5e4b0

Update api/endpoints/location.py

Browse files
Files changed (1) hide show
  1. api/endpoints/location.py +10 -3
api/endpoints/location.py CHANGED
@@ -1,6 +1,7 @@
1
  from fastapi import APIRouter, HTTPException , Body,Query
2
  from models.location_models import BodyData,ErrorResponse
3
  from services.location_service import LocationService
 
4
  import core.init_supabase as sp
5
 
6
  router = APIRouter()
@@ -18,9 +19,15 @@ async def get_coordinates(user_id: str = Query(..., description="User's hush ID"
18
  print(supabase_user_data)
19
  print(len(supabase_user_data))
20
  coords=[]*len(supabase_user_data)
21
- for cord in supabase_user_data:
22
- result = LocationService.get_coordinates(cord)
23
- coords.append(result)
 
 
 
 
 
 
24
  if isinstance(result, ErrorResponse):
25
  print(HTTPException(status_code=400, detail=result.error))
26
  return {"message":"An unexpected error occured please try again !!"}
 
1
  from fastapi import APIRouter, HTTPException , Body,Query
2
  from models.location_models import BodyData,ErrorResponse
3
  from services.location_service import LocationService
4
+ from concurrent.futures import ThreadPoolExecutor, as_completed
5
  import core.init_supabase as sp
6
 
7
  router = APIRouter()
 
19
  print(supabase_user_data)
20
  print(len(supabase_user_data))
21
  coords=[]*len(supabase_user_data)
22
+ with ThreadPoolExecutor(max_workers=15) as executor:
23
+ future_to_cord = {executor.submit(get_coordinates, cord): cord for cord in supabase_user_data}
24
+ for future in as_completed(future_to_cord):
25
+ cord = future_to_cord[future]
26
+ try:
27
+ result = future.result()
28
+ coords.append(result)
29
+ except Exception as exc:
30
+ print(f'Coordinate {cord} generated an exception: {exc}')
31
  if isinstance(result, ErrorResponse):
32
  print(HTTPException(status_code=400, detail=result.error))
33
  return {"message":"An unexpected error occured please try again !!"}