← All examples

Batch processing

Process ten thousand inputs with .map()

Decorate a function, call .map() over ten thousand inputs, and each one runs in its own container. Warm starts are sub-second because the image is cached on the worker, and the pool scales back to zero when the queue drains.

CPU containers by default; add gpu= to fan out onto GPUs the same way.

How it works

  1. 1.remote(x) runs the function once, in a container, and returns the result; .map(iterable) does it once per item and streams results back in order of completion.
  2. 2The controlplane keeps a queue per function and starts containers as capacity allows; a container serves many items back to back while the queue is non-empty.
  3. 3retries= and timeout= handle failures per item; the dashboard shows every item as a task with its own logs.
  4. 4levers9 deploy turns the same function into a URL you can POST to from anywhere.

The code

demo/cpu_job/app.py
from levers9 import job

@job(cpu=1, memory="512Mi", retries=3, timeout=600)
def is_prime(n: int) -> dict:
    ...

if __name__ == "__main__":
    print(is_prime.remote(7919))                     # one container
    for r in is_prime.map(range(10_000)):            # one container per item, as capacity allows
        ...
run it
cd demo/cpu_job
python app.py
levers9 deploy app.py:is_prime --name is-prime        # POST /job/is-prime/v1

Where to go next

Related