First task¶
Run a Python function and a bash script on Nebius as if they were local calls, and pass the result of one to the next.
View example files ↗: task_quickstart.py · Related reference: Tasks →
"""The task quickstart: run a Python function and a bash script as jobs.
Each task call is one small CPU job. Arguments and results travel
through the project's task bucket, which the first run creates. Reading
the results here needs Object Storage credentials: ``AWS_ACCESS_KEY_ID``
and ``AWS_SECRET_ACCESS_KEY``, or a profile in ``~/.aws/credentials``.
Uses the active Nebius profile.
"""
import nebius_serverless as ns
IMAGE = "python:3.12-slim-bookworm"
TEXT = "to be or not to be that is the question"
def count_words(text: str) -> dict[str, int]:
"""Count each word in a text. Runs in the job, so it imports there."""
from collections import Counter
return dict(Counter(text.split()))
def most_common(counts: dict[str, int]) -> str:
"""Return the most frequent word."""
return max(counts, key=lambda word: counts[word])
count = ns.Task(count_words, image=IMAGE, timeout="10m")
top = ns.Task(most_common, image=IMAGE, timeout="10m")
# A bash script. Its inputs arrive in the bash array ``inputs``; what it
# sets in ``outputs`` comes back as a dict of strings.
hello = ns.Task(
name="hello",
image=IMAGE,
inputs=["who"],
command="""
echo "hello ${inputs[who]}"
outputs[greeting]="hello ${inputs[who]}"
outputs[python]=$(python3 --version)
""",
timeout="10m",
)
def main() -> None:
"""Call each task, then chain two of them."""
# Calling a task starts its job, waits, and returns the result.
print(count(text=TEXT))
print(hello(who="world"))
# submit() returns at once. A run passed to another task is replaced
# by its result, and that task's job starts once the run has ended.
counts = count.submit(text=TEXT)
word = top.submit(counts=counts)
print(f"most common: {word.result()}")
if __name__ == "__main__":
main()
Object Storage credentials
Results come back through a bucket in your project, and this machine reads it over S3. Set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY to a Nebius Object Storage access key, or put one in ~/.aws/credentials.
What happens¶
ns.Task(count_words, image=..., timeout="10m")wraps the function. The other options are the onesJobSpectakes:gpu,cpu,pricing,env,secrets,volumes, and the rest. Nothing contacts Nebius yet.count(text=TEXT)starts a job, waits for it, and returns what the function returned. The function's source is sent to the job, so it must be self-contained: imports go inside its body.hello(who="world")runs a bash script. Its inputs arrive in the bash arrayinputs, and what it sets in the arrayoutputscomes back as a dict of strings.count.submit(...)returns a run at once.top.submit(counts=counts)gets that run as an argument: its job starts whencountshas finished, with the result in its place.word.result()waits for the job and returns the result.
The first run creates the bucket ns-workspace-<project id> in the project (Files and the task bucket). Every task's job mounts it at the same path, and each run keeps its arguments, result, and files in tasks/<task>/<time>/.
Arguments and results¶
Arguments and return values are JSON values: numbers, strings, lists, dicts, None. For anything larger, write files to $NS_OUTPUT_DIR in the job and return their path. The next task can open that path, because every job sees the bucket in the same place.
A function that raises, a script that exits non-zero, or a job that ends without a result raises ns.TaskFailed. It carries the job id, the traceback from the job (remote_traceback), and for a script its exit_code and the outputs set so far.
Your own code¶
A function defined in your script is sent as source. To use a package from your project, list it in local_libs: it is uploaded once per change, and the job imports the function by name.
from mylib.training import train_model
train = ns.Task(train_model, image=image, local_libs=["mylib"], gpu="h100", timeout="2h")
image can be an ns.Image: it is built on the first run and reused after that.
Next¶
Run tasks as a Prefect flow, or see a full pipeline of tasks that trains and evaluates a model. Every option is in the Tasks reference.