Skip to main content

Running a Python Script

stars - badge-generator

This example tutorial serves as an introduction to Bacalhau. Here, you'll be running a Python file hosted on a website on Bacalhau.

TD;LR

A quick guide on how to run a hello world script on Bacalhau

Prerequisites

To get started, you need to install the Bacalhau client, see more information here

Creating a Hello World File

We'll be using a very simple Python script that displays the traditional first greeting.

%cat hello-world.py

Submit the workload

To submit a workload to Bacalhau you can use the bacalhau docker run command.

%%bash --out job_id
bacalhau docker run \
--id-only \
--input https://raw.githubusercontent.com/bacalhau-project/examples/151eebe895151edd83468e3d8b546612bf96cd05/workload-onboarding/trivial-python/hello-world.py \
python:3.10-slim -- python3 /inputs/hello-world.py

When a job is submitted, Bacalhau prints out the related job_id. We store that in an environment variable so that we can reuse it later on.

%env JOB_ID={job_id}

The bacalhau docker run command allows passing input data into the container using content identifier (CID) volumes, we will be using the -i URL:path argument for simplicity. This results in Bacalhau mounting a data volume inside the container. By default, Bacalhau mounts the input volume at the path /inputs inside the container.

info

Bacalhau overwrites the default entrypoint, so we must run the full command after the -- argument.

Checking the State of your Jobs

  • Job status: You can check the status of the job using bacalhau list.
%%bash
bacalhau list --id-filter=${JOB_ID} --no-style

When it says Published or Completed, that means the job is done, and we can get the results.

  • Job information: You can find out more information about your job by using bacalhau describe.
%%bash
bacalhau describe ${JOB_ID}
  • Job download: You can download your job results directly by using bacalhau get. Alternatively, you can choose to create a directory to store your results. In the command below, we created a directory and downloaded our job output to be stored in that directory.
%%bash
rm -rf results && mkdir results
bacalhau get ${JOB_ID} --output-dir results

Viewing your Job Output

To view the file, run the following command:


%%bash
cat results/stdout

Need Support?

For questions, and feedback, please reach out to our forum