Developer docs / AI Fabric tutorials
Async jobs: submit now, get the result later
- Async job
- Web endpoint
Take it for a spin
The whole program in one file, ready to run.
import bladekit
app = bladekit.app("async-batch", image="fabric/base")
@app.web_endpoint(path="/crunch", methods=["POST"], min_replicas=1, max_replicas=3)
def crunch(item):
import time
n = int(item.get("n", 5))
time.sleep(n) # stand-in for real work
return {"crunched": n, "sum": sum(range(n * 1000))}
@app.local_entrypoint()
def main():
# spawn() returns immediately with a handle: the connection is freed and
# the job keeps running on the endpoint.
job = bladekit.Function.from_name("async-batch", "/crunch").spawn({"n": 6})
print("submitted:", job.call_id, "status:", job.status())
# Stash job.call_id anywhere (a DB, a queue). Reconnect from ANY process:
result = bladekit.DeployedFunctionCall.from_id(job.call_id).get(timeout=60)
print("RESULT:", result)Run it
- Deploy the endpoint:
blade deploy async_batch.py. spawn()POSTs/v1/serve-async/...: the control plane enqueues the call and answers 202 with acall_idWITHOUT holding the connection. No more gateway timeouts on long jobs.handle.status()→ queued/running/completed;handle.get(timeout=)blocks until done and returns the endpoint's JSON body.DeployedFunctionCall.from_id(call_id)re-attaches to the job from a different process. Thecall_idis the durable handle.- Needs the AI Fabric SDK ≥ 0.5.2 on the client (
pip install -U bladekit).
Concepts used: Async job, Endpoint, Replica, Autoscaling