Skip to content

Add batch async search helper (async_search) - #56

Open
AusafKhan144 wants to merge 3 commits into
serpapi:masterfrom
AusafKhan144:async-search-batch-helper
Open

AusafKhan144 wants to merge 3 commits into
serpapi:masterfrom
AusafKhan144:async-search-batch-helper

Conversation

@AusafKhan144

Copy link
Copy Markdown

This is a first contribution, addressing #53 ("Make async: True batch searches work out of the box").

What it does
Adds Client.async_search(), which runs a batch of async searches end to end: it submits each search with async: True via the existing Client.search(), then polls the Search Archive API via the existing Client.search_archive() until every search reaches a terminal status (Success, Cached, or Error), and returns the results as a list in the same order as the input. Archive polls send only search_id, as the issue notes — reusing the original params makes the Archive API error.

API

  • client.async_search(queries=["burly", "creator", "doubtful", "minecraft"], engine="youtube", device="desktop") — the shape proposed in the issue works verbatim; shared kwargs apply to every search.
  • It also accepts a list of full parameter dicts (searches=[{...}, ...]) for batches that mix engines or params; per-search values win over shared kwargs. Plain strings are shorthand for {"q": item}.
  • A search that fails to submit/retrieve, or is still unfinished at the deadline, yields the exception instance in its slot (like asyncio.gather(return_exceptions=True)) instead of sinking the whole batch. Error-status searches return their SerpResults, which carry the API's error message.
  • poll_interval (default 1s) and timeout (default 300s; None waits indefinitely) are keyword-only. Caller dicts are never mutated.

Tests
11 new tests in tests/test_async_search.py (mocked HTTP, in the repo's existing style) covering: all succeed, order preservation when searches finish out of order, polling until Success, one failed submission isolated while siblings succeed, Error status returned as a result, the queries= call shape, archive requests carrying only search_id + api_key, timeout behavior, input-dict immutability, empty batch, and missing-argument TypeError. Full offline suite: 250 passed vs a 239 baseline before this change, no regressions.

Deliberate choices

  • Broader input than the issue sketched (full param dicts as well as queries=), since real batch jobs mix engines/params.
  • Sync Client only: AsyncClient users can already fan out with asyncio.gather; an asyncio polling variant deserves its own design.
  • Default 300s overall timeout so a stuck search can't hang a script forever.

Happy to adjust the API shape or split anything out if maintainers prefer.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant