Developer docs / AI Fabric tutorials

Fire-and-forget fan-out with spawn_map

  • Batch / map
  • Async job

Take it for a spin

The whole program in one file, ready to run.

spawn_map.py
import bladekit

app = bladekit.app("spawn-map")

@app.function(image="fabric/base", timeout=120)
def square(x: int) -> int:
    return x * x

@app.local_entrypoint()
def main():
    items = list(range(8))

    # spawn_map returns a MapHandle at once; nothing is awaited here.
    handle = square.spawn_map(items)
    print(f"Fan-out launched: {handle.map_id}")

    # Non-blocking: the batch is already running server-side, so we can poll
    # its status before we ever ask for results.
    print(handle.status()["outcome_counts"])

    # ... do other work while the fleet churns through the batch ...

    # Collect results whenever you're ready (order preserved by default).
    results = list(handle.results(return_exceptions=True))
    print(f"Squares: {results}")

Run it

  1. Save as spawn_map.py. square.spawn_map(items) creates the batch, pushes every input, and finalizes it server-side, then returns a MapHandle immediately.
  2. Unlike .map() (which blocks and streams results as they land), spawn_map never blocks: you get the handle and get on with other work.
  3. Collect later with handle.results(order=..., return_exceptions=...), check progress with handle.status(), or stop it with handle.cancel().
  4. The batch is durable server-side, so the handle survives a client disconnect. Re-attach from any process with blade map resume <map_id>.
  5. Use spawn_starmap when each element is a tuple of positional args: (a, b) maps to f(a, b).

Concepts used: Run, Async job

Contact the team

Loading the form…