perf: unblock event loop for segment_efforts scan
Extract the synchronous segment-file scan into a plain function and dispatch it via asyncio.to_thread so it runs in a thread pool instead of blocking the event loop during concurrent fetches.
This commit is contained in:
@@ -351,13 +351,16 @@ async def activity_segment_efforts(
|
||||
bincio_session: str | None = Cookie(default=None),
|
||||
) -> JSONResponse:
|
||||
"""Return segment efforts that belong to a specific activity for the logged-in user."""
|
||||
import asyncio
|
||||
from bincio.segments import store as _seg_store
|
||||
user = deps._require_user(bincio_session)
|
||||
dd = deps._get_data_dir()
|
||||
|
||||
def _collect() -> list[dict]:
|
||||
efforts_dir = dd / user.handle / "segment_efforts"
|
||||
result = []
|
||||
if efforts_dir.exists():
|
||||
import json as _json
|
||||
result: list[dict] = []
|
||||
if not efforts_dir.exists():
|
||||
return result
|
||||
for ef_file in sorted(efforts_dir.glob("*.json")):
|
||||
seg_id = ef_file.stem
|
||||
all_efforts = _seg_store.load_efforts(dd, user.handle, seg_id)
|
||||
@@ -377,4 +380,6 @@ async def activity_segment_efforts(
|
||||
"pr_elapsed_s": pr_elapsed,
|
||||
"started_at": _seg_store._iso(eff.started_at),
|
||||
})
|
||||
return JSONResponse(result)
|
||||
return result
|
||||
|
||||
return JSONResponse(await asyncio.to_thread(_collect))
|
||||
|
||||
Reference in New Issue
Block a user