First Prefect flow¶
Run a pipeline of tasks as a Prefect flow. The flow itself runs on a small Nebius job, so it keeps going after your script ends, and every task is a Prefect task run with a link to its job.
View example files ↗: flow_quickstart.py · Related reference: Prefect →
"""The Prefect flow quickstart: a pipeline of tasks as a Prefect flow.
``submit()`` starts one CPU job that runs ``pipeline`` as a Prefect flow,
and that job starts the tasks' jobs. The script prints the flow run's
link, then waits for the result.
Set ``PREFECT_API_URL`` to your Prefect server's API address, such as
``https://prefect.example.com/api``, and ``PREFECT_API_AUTH_STRING`` to
``user:password`` if it asks for a login. Prefect itself is not needed
on this machine. Reading the result needs the same Object Storage
credentials as the task quickstart. Uses the active Nebius profile.
"""
import os
import nebius_serverless as ns
from nebius_serverless.integrations.prefect import PrefectFlow
IMAGE = "python:3.12-slim-bookworm"
def square_number(x: int) -> int:
"""Return ``x`` squared."""
return x * x
def add_numbers(values: list[int]) -> int:
"""Return the sum of ``values``."""
return sum(values)
square = ns.Task(square_number, name="square", image=IMAGE, timeout="10m")
add = ns.Task(add_numbers, name="add", image=IMAGE, timeout="10m")
def pipeline(n: int = 3) -> int:
"""Square 0 to n-1, one job each at the same time, then add them up."""
squares = [square.submit(x=x) for x in range(n)]
return add(values=squares)
def main() -> None:
"""Start the flow on a Nebius job and wait for its result."""
flow = PrefectFlow(
pipeline,
flow_name="sum-of-squares",
api_url=os.environ.get("PREFECT_API_URL"),
timeout="1h", # the flow job's limit
)
run = flow.submit(n=3)
print(f"sum of squares: {run.result()}")
if __name__ == "__main__":
main()
export PREFECT_API_URL=https://prefect.example.com/api
uv run python examples/quickstarts/flow_quickstart.py
You need a Prefect server, and the same Object Storage credentials as in First task. PrefectFlow comes from nebius_serverless.integrations.prefect; Prefect itself does not have to be installed on your machine.
What happens¶
pipelineis a plain function that calls tasks. It needs no@flowdecorator.PrefectFlow(pipeline, flow_name=..., timeout="1h")checks thatpipelineuses only tasks, constants with JSON values, and what it imports inside its body. Nothing contacts Nebius yet.submit(n=3)checks the Prefect server and its login, creates the flow run in Prefect, and prints the link to it. It then builds the tasks' images and uploads their libraries. The first submit in a project also builds the flow job's image, Python with Prefect, once.- It starts the flow job on the smallest CPU machine and returns its run. The flow job runs
pipeline, and starts one job per task call. run.result()waits for the flow job and returns whatpipelinereturned.
The run is named by its launch time, such as 20261125-091833-sum-of-squares, in Prefect, as the flow job, and as its folder flows/sum-of-squares/<run>/ in the task bucket.
Prefect login¶
When the server asks for a login, set PREFECT_API_AUTH_STRING to user:password, or put that line in ~/.config/prefect/auth. submit checks it with the server first, so a wrong login fails at once and not inside the job.
The flow job starts jobs as you. A short-lived access token, and the Prefect login when there is one, are stored as a new version of the generated-tasks-credentials MysteryBox secret and reach the job as environment variables.
Run the flow here¶
remote_job=False runs the flow in your process instead of on a job. The tasks still run on Nebius. This needs Prefect here, pip install "nebius-serverless[prefect]", and the script stays open until the flow ends:
flow = PrefectFlow(pipeline, flow_name="sum-of-squares", remote_job=False)
run = flow.submit(n=3)
print(run.flow_run_url)
print(run.result())
Next¶
See a full pipeline of tasks, or everything about the Prefect integration.