
Give a Python media poller two separate limits: an HTTP timeout for each status request and an overall budget for how long this invocation will wait. When either limit is reached, preserve the existing Imagild task ID. A local exception is not evidence that the accepted generation failed.
Use an HTTP client such as HTTPX for raw authenticated requests. The Imagild API console defines the task envelope; this example does not depend on an image-model SDK.
Key Takeaways
- An HTTP inactivity timeout and a polling deadline answer different questions.
- Read the saved task instead of POSTing again after a slow status request.
- Return a resumable local outcome without overwriting the last remote status.
Understand the timeout types
HTTPX documents connect, read, write and pool timeouts. A read timeout limits waiting for incoming data; it is not a promise that the entire generation completes within that interval.
For an async Python 3.11-or-newer worker, an outer asyncio.timeout can bound an individual wait by the remaining overall budget. The Python asyncio reference documents that cancellation-based timeout context.
The following reader assumes HTTPX is already part of your environment. Load the API key from secure server configuration and pass a real saved task ID.
import asyncio
import time
import httpx
async def poll_task(task_id, api_key, budget_seconds=120):
deadline = time.monotonic() + budget_seconds
delay = 5.0
last_task = None
timeout = httpx.Timeout(15.0, connect=5.0, pool=5.0)
async with httpx.AsyncClient(timeout=timeout) as client:
while True:
remaining = deadline - time.monotonic()
if remaining <= delay:
break
await asyncio.sleep(delay)
try:
async with asyncio.timeout(deadline - time.monotonic()):
response = await client.get(
"https://imagild.com/api/relay/v1/tasks/" + task_id,
headers={"Authorization": "Bearer " + api_key},
)
except TimeoutError:
break
except httpx.TimeoutException:
return {"local": "http-timeout", "task_id": task_id,
"last_task": last_task}
if response.status_code == 429:
try:
delay = max(5.0, float(response.headers["Retry-After"]))
except (KeyError, ValueError):
delay = 60.0
continue
response.raise_for_status()
task = response.json()["data"]
if task["id"] != task_id:
raise ValueError("Unexpected task identity")
last_task = task
if task["status"] in {"succeeded", "failed", "unknown"}:
return {"local": "outcome", "task": task}
delay = max(5.0, float(task.get("poll_after_seconds", 5)))
return {"local": "wait-budget-ended", "task_id": task_id,
"last_task": last_task}
The two-minute budget and HTTP limits are example client choices, not Imagild performance guarantees. Validate persisted task IDs as UUIDs in your application. Handle JSON, protocol and access errors outside the reader while retaining the operation record.
Keep local outcomes out of provider fields
The returned local values belong to this Python helper, not to Imagild's API. If it returns http-timeout, do not save failed as the task's new remote status.
On a reported succeeded task, begin the separate authenticated download. On failed, inspect data.error and usage. On unknown, stop automatic replacements and request reconciliation. An HTTP 200 alone cannot choose among these branches.
A valid key and active membership remain necessary for every status read. A 401 or membership-related 403 is an access problem rather than proof of generation failure.
Keep scheduling bounded across tasks
The current account allowance is 120 reads per minute. Giving each of many workers a five-second loop can exceed that shared allowance, even if every loop looks reasonable in isolation.
Coordinate polling centrally or increase intervals as your workload grows. Respect Retry-After rather than immediately spinning on a rate-limited task. Your scheduling policy must not create additional generation operations.
Verify resume behavior with a fake server
Test a processing response followed by a deliberately delayed read. Confirm that the function returns or raises a local read problem while the same task ID survives. Resume later and return succeeded without another generation POST.
Also test failed, unknown and 429 responses. Use the failed-job checklist for recovery messaging and the points planning guide to keep a deliberate retake separate from recovering existing work.
Related tasks
- What Should You Check Before Retrying a Failed Image or Video Job?
- Imagild Points, Resolution and Jobs: Plan a Creation Before You Spend
Sources and scope
- Imagild API console: Active-member access, supported media request fields, idempotency, asynchronous task records and authenticated downloads; checked against the 2026-10-28 relay contract.
- HTTPX timeouts: Connect, read, write and pool timeout semantics rather than whole generation duration.
- Python asyncio timeouts: Python 3.11+ timeout context and local cancellation behavior.
Product instructions were checked against the current Imagild API contract and the public model catalog. Code, budgets and acceptance checks are illustrative; this article does not claim a new paid generation test, an uptime measurement, a search-volume study or a ranking of AI citations.
Prepared by Imagild Editorial, the company editorial team responsible for these guides. AI assisted research and drafting. This is documentation-based guidance with proposed examples, not a claim of hands-on testing or an independent product review. Report a correction.
Cover: Original Imagild product screenshot captured on 28 October 2026; model availability and prices may change. This image is not an API response or a performance benchmark.