⚡ Bolt: Optimize CPU move selection to be non-blocking#15
⚡ Bolt: Optimize CPU move selection to be non-blocking#15
Conversation
- Wrapped `cpu_pick_move` calls in `backend/realtime/cpu.py` with `asyncio.to_thread` to prevent blocking the event loop. - Fixed `fixed_do_toss` mock signature in `backend/test_game_engine.py` to fix unrelated test failure. Co-authored-by: Horus0305 <98160215+Horus0305@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Pull request overview
This PR optimizes CPU move selection in the real-time WebSocket server by offloading blocking database operations to a thread pool using asyncio.to_thread(). The cpu_pick_move function performs synchronous database queries to fetch user patterns and match history, which previously blocked the asyncio event loop and caused latency spikes for all connected clients. By running these operations in separate threads, the event loop remains responsive to other concurrent events.
Changes:
- Wrapped all
cpu_pick_movecalls withasyncio.to_thread()to prevent event loop blocking - Fixed test mock function signature to match the actual
do_tossmethod signature
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
CricketGame/backend/realtime/cpu.py |
Wrapped all four cpu_pick_move invocations (in maybe_cpu_move and auto_play_cpu_match) with asyncio.to_thread() to offload blocking database operations to a thread pool |
CricketGame/backend/test_game_engine.py |
Fixed mock function fixed_do_toss signature to accept optional caller parameter, matching the actual Match.do_toss method signature |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Horus0305 <98160215+Horus0305@users.noreply.github.com>
💡 What:
Wrapped the synchronous
cpu_pick_movecalls inbackend/realtime/cpu.pywithasyncio.to_thread. This offloads the database-intensive CPU strategy logic to a separate thread.🎯 Why:
The
cpu_pick_movefunction performs synchronous database queries (fetching user patterns, match history) which block the main asyncio event loop. In a real-time WebSocket server, this blocking causes latency spikes for all connected clients whenever a CPU makes a move.📊 Impact:
Eliminates event loop blocking during CPU moves. This ensures the server remains responsive to other events (pings, human moves) even when complex CPU strategy calculations are running.
🔬 Measurement:
Verified using
test_cpu_async_verify.py(which mocked the behavior and asserted async execution) and by running the existing test suitebackend.test_game_engine(which passed after a minor fix to a mock function signature).PR created automatically by Jules for task 10175184459672933676 started by @Horus0305