perf: replace blocking requests.get with aiohttp in get_player_headshot #100

Open
opened 2026-03-20 12:36:12 +00:00 by cal · 0 comments
Owner

Problem

get_player_headshot() in helpers/main.py (~line 73) uses synchronous requests.get() to scrape headshot images from baseball-reference.com. This blocks the entire bot event loop for up to 2 seconds per card that's missing a cached headshot.

While this only triggers on cache miss (first display of a new player), when opening packs with new players it can add significant delay and freeze all other bot operations.

Proposed Fix

Replace requests.get(req_url, timeout=2) with aiohttp.ClientSession (already used elsewhere in the codebase via api_calls.py). Run in an async context so the event loop isn't blocked.

Impact

Prevents event loop stalls on headshot cache misses. Critical for new cardset releases where many players won't have cached headshots yet.

## Problem `get_player_headshot()` in `helpers/main.py` (~line 73) uses synchronous `requests.get()` to scrape headshot images from baseball-reference.com. This **blocks the entire bot event loop** for up to 2 seconds per card that's missing a cached headshot. While this only triggers on cache miss (first display of a new player), when opening packs with new players it can add significant delay and freeze all other bot operations. ## Proposed Fix Replace `requests.get(req_url, timeout=2)` with `aiohttp.ClientSession` (already used elsewhere in the codebase via `api_calls.py`). Run in an async context so the event loop isn't blocked. ## Impact Prevents event loop stalls on headshot cache misses. Critical for new cardset releases where many players won't have cached headshots yet.
cal added the
ai-working
performance
labels 2026-03-20 12:36:24 +00:00
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: cal/paper-dynasty-discord#100
No description provided.