un-inception/clients/python/async/examples/async_job_polling.py
russell@unturf.com 1e01d09883 feat: per-client Makefile infrastructure for 4-mode testing
Add per-client Makefiles for C, Python, and Go with:
- CLI mode: Tests --help, arg parsing, syntax validation
- Library mode: Unit tests, import verification
- Integration mode: API contract validation (with credentials)
- Functional mode: Real-world scenario tests

C client:
- 22 library tests (SHA-256, HMAC-SHA256, detect_language)
- Full unsandbox.c implementation with examples

Python client (sync + async):
- Delegates to sync/ and async/ subdirectories
- pytest-based test suites with coverage
- Examples for concurrent execution, streaming

Go client:
- Delegates to sync/ and async/ subdirectories
- go test integration with vet and fmt

Also update detect-changes.sh to detect changes in
both root-level un.* files AND clients/ directory.
2026-01-15 16:39:56 -05:00

58 lines
1.6 KiB
Python

#!/usr/bin/env python3
"""
Async job polling example - demonstrates fire-and-forget execution
This example shows how to:
1. Start jobs asynchronously with execute_async()
2. Poll job status with get_job()
3. Wait for completion with wait_for_job()
4. Cancel jobs with cancel_job()
Usage:
python async_job_polling.py
Or with custom credentials:
UNSANDBOX_PUBLIC_KEY=... UNSANDBOX_SECRET_KEY=... python async_job_polling.py
"""
import asyncio
import sys
import os
# Add src to path for development
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
from un_async import execute_async, get_job, wait_for_job, list_jobs
async def main():
"""Demonstrate async job operations."""
try:
print("1. Starting async job...")
job_id = await execute_async("python", 'print("Job result")')
print(f" Job ID: {job_id}")
print("\n2. Checking job status...")
job_status = await get_job(job_id)
print(f" Status: {job_status.get('status')}")
print("\n3. Waiting for job completion...")
result = await wait_for_job(job_id)
print(f" Final status: {result.get('status')}")
print(f" Output: {result.get('stdout', '').strip()}")
print("\n4. Listing all jobs...")
jobs = await list_jobs()
print(f" Total jobs: {len(jobs)}")
if jobs:
print(f" Most recent job ID: {jobs[0].get('id')}")
return 0
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
return 1
if __name__ == "__main__":
exit_code = asyncio.run(main())
sys.exit(exit_code)