Milestone

Completed

Improvements

Like anything, there's room for improvement

Parallelising the pull

The Last.fm API is rate‑limited and the script currently sleeps between calls, so parallelism helps if we restructure the workflow to reduce round‑trip latency, not to increase request volume.

Currently the api_call() is written to be called serially, one week at a time, with a flat --delay between calls to stay under Last.fm’s 5 requests/second/IP limit. That’s easy to reason about but leaves speed on the table. A full ~1,120-week pull spends most of its time waiting rather than fetching.

The current script:

  1. Fetches week list
  2. For each week:
    • call weekly artist chart
    • sleep
    • write results

This means the total runtime is: weeks Ɨ (APIĀ latency + delay)

For a user with ~800 weeks of history, that’s easily 10–20 minutes.

Where async helps

Async improves speed when:

  • you have many independent API calls
  • each call has non‑trivial latency (Last.fm often takes 200–500ms)
  • you can pipeline requests without exceeding rate limits
  • this script fits this pattern.

Where async doesn’t help

If you simply fire off 800 requests at once, Last.fm will:

  • return 429 rate limit errors
  • possibly ban your API key temporarily

So the async version must still throttle requests. Enter asynchio semaphores. I’m not going to go into any depth for this as it’s something I’m new to and clearly there’s plenty to read, that said, it’s really interesting!

Back to this, this script pulls once, writes a file, and the file gets checked in. There’s no server component and nothing here is about scaling it up to run anywhere but a laptop.

Further reading

I came across this interesting blog post about asynchio semaphores and the handling of concurrent processes. Like I said, this is a topic for another time. I want some time to look into it and play.