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.

async_batch.py
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

  1. Deploy the endpoint: blade deploy async_batch.py.
  2. spawn() POSTs /v1/serve-async/...: the control plane enqueues the call and answers 202 with a call_id WITHOUT holding the connection. No more gateway timeouts on long jobs.
  3. handle.status() → queued/running/completed; handle.get(timeout=) blocks until done and returns the endpoint's JSON body.
  4. DeployedFunctionCall.from_id(call_id) re-attaches to the job from a different process. The call_id is the durable handle.
  5. Needs the AI Fabric SDK ≥ 0.5.2 on the client (pip install -U bladekit).

Concepts used: Async job, Endpoint, Replica, Autoscaling

Contact the team

Loading the form…