The async API pattern is ideal when you want to submit multiple long-running tasks and check their status later. Instead
of waiting for each run to complete, you submit all requests and get back run IDs that you can poll for completion.
Submit requests to /v1/automation/run-async, which returns corresponding run_ids, which you will need if you want
to check the status of a particular run.
Check individual runs with GET /v1/runs/:id to check status
Or fetch all runs with GET /v1/runs to monitor batch progress
Submit multiple TinyFish Web Agent runs and poll for completion:
import asynciofrom tinyfish import AsyncTinyFish, RunStatusasync def wait_for_completion(client, run_id, poll_interval=2): """Poll a run until it completes""" while True: run = await client.runs.get(run_id) if run.status in (RunStatus.COMPLETED, RunStatus.FAILED, RunStatus.CANCELLED): return run await asyncio.sleep(poll_interval)async def main(): client = AsyncTinyFish() # Define your batch of tasks tasks_to_run = [ { "url": "https://scrapeme.live/shop/", "goal": "Extract all available products on page two with their name, price, and review rating (if available)", }, { "url": "https://books.toscrape.com/", "goal": "Extract all available books on page two with their title, price, and review rating (if available)", }, ] # Step 1: Submit all tinyfish runs and collect run_ids print("Submitting tinyfish runs...") submit_tasks = [ client.agent.queue(url=task["url"], goal=task["goal"]) for task in tasks_to_run ] responses = await asyncio.gather(*submit_tasks) run_ids = [r.run_id for r in responses] print(f"Submitted {len(run_ids)} runs: {run_ids}") # Step 2: Wait for all runs to complete print("Waiting for completion...") completion_tasks = [ wait_for_completion(client, run_id) for run_id in run_ids ] results = await asyncio.gather(*completion_tasks) # Step 3: Process results for i, run in enumerate(results): print(f"Run {i + 1} ({run.run_id}):") print(f" Status: {run.status}") if run.status == RunStatus.COMPLETED: print(f" Result: {run.result}")# Run the async main functionasyncio.run(main())