Browse Source

Adding multi-env exanple

tags/v0.3.11-rc1
haixuanTao 10 months ago
parent
commit
722b95a0b0
4 changed files with 103 additions and 0 deletions
  1. +1
    -0
      examples/python-multi-env/.gitignore
  2. +10
    -0
      examples/python-multi-env/README.md
  3. +32
    -0
      examples/python-multi-env/dataflow.yml
  4. +60
    -0
      examples/python-multi-env/run.rs

+ 1
- 0
examples/python-multi-env/.gitignore View File

@@ -0,0 +1 @@
*.pt

+ 10
- 0
examples/python-multi-env/README.md View File

@@ -0,0 +1,10 @@
# Python Dataflow Example

This examples shows how to connect two different python virtual env with python.

```bash
uv venv --seed -p 3.11 -n env_1
uv venv --seed -p 3.11 -n env_2
dora build dataflow.yml --uv
dora run dataflow.yml --uv
```

+ 32
- 0
examples/python-multi-env/dataflow.yml View File

@@ -0,0 +1,32 @@
nodes:
- id: camera
build: pip install -e ../../node-hub/opencv-video-capture
path: opencv-video-capture
inputs:
tick: dora/timer/millis/20
outputs:
- image
env:
CAPTURE_PATH: 0
IMAGE_WIDTH: 640
IMAGE_HEIGHT: 480
VIRTUAL_ENV: env_1

- id: object-detection
build: pip install -e ../../node-hub/dora-yolo
path: dora-yolo
inputs:
image: camera/image
outputs:
- bbox
env:
VIRTUAL_ENV: env_2

- id: plot
build: pip install dora-rerun
path: dora-rerun
inputs:
image: camera/image
boxes2d: object-detection/bbox
env:
VIRTUAL_ENV: env_1

+ 60
- 0
examples/python-multi-env/run.rs View File

@@ -0,0 +1,60 @@
use dora_core::{get_uv_path, run};
use dora_tracing::set_up_tracing;
use eyre::{bail, WrapErr};
use std::path::Path;

#[tokio::main]
async fn main() -> eyre::Result<()> {
set_up_tracing("python-dataflow-runner")?;

let root = Path::new(env!("CARGO_MANIFEST_DIR"));
std::env::set_current_dir(root.join(file!()).parent().unwrap())
.wrap_err("failed to set working dir")?;

let uv = get_uv_path().context("Could not get uv binary")?;

run(&uv, &["venv", "-p", "3.10", "--seed"], None)
.await
.context("failed to create venv")?;

run(
&uv,
&[
"pip",
"install",
"-e",
"../../apis/python/node",
"--reinstall",
],
None,
)
.await
.context("Unable to install develop dora-rs API")?;

let dataflow = Path::new("dataflow.yml");
run_dataflow(dataflow).await?;

Ok(())
}

async fn run_dataflow(dataflow: &Path) -> eyre::Result<()> {
let cargo = std::env::var("CARGO").unwrap();

// First build the dataflow (install requirements)
let mut cmd = tokio::process::Command::new(&cargo);
cmd.arg("run");
cmd.arg("--package").arg("dora-cli");
cmd.arg("--").arg("build").arg(dataflow).arg("--uv");
if !cmd.status().await?.success() {
bail!("failed to run dataflow");
};

let mut cmd = tokio::process::Command::new(&cargo);
cmd.arg("run");
cmd.arg("--package").arg("dora-cli");
cmd.arg("--").arg("run").arg(dataflow).arg("--uv");
if !cmd.status().await?.success() {
bail!("failed to run dataflow");
};
Ok(())
}

Loading…
Cancel
Save