Skip to main content

Quick Start

Install​

Raio runs on Linux and macOS.

Python 3.9 or later:

pip install raio-py

A recent stable Rust toolchain:

cargo new hello && cd hello
cargo add raio-rs

Hello World​

hello.py
import raio
from raio import MS, InputPort, Node, OutputPort, reaction, task

# A node runs in its own process.
class Sender(Node):
# Nodes communicate through ports.
# An OutputPort can send values to InputPorts.
out = OutputPort[str]()

# When a node starts, its tasks are executed.
@task
async def send(self):
await raio.wait(1 * MS) # one millisecond of logical time
self.out.set("hello world") # send a value through the output port

class Listener(Node):
# InputPorts receive values. [str] is a type annotation.
inp = InputPort[str]()

# A reaction runs each time its port receives a value.
@reaction(inp)
def show(self):
print(self.inp.value) # read input port values with .value

sender, listener = Sender(), Listener()
raio.connect(sender.out, listener.inp) # port connections are type checked
raio.run(sender, listener) # run the nodes until all of them finish
src/main.rs
use raio::app::{App, Builder, InputPort, Node, OutputPort, Ports, node};
use raio::time::MS;

// A node runs in its own process.
#[derive(Ports)]
struct Sender {
node: Node,
// Nodes communicate through ports.
// An OutputPort can send values to InputPorts.
out: OutputPort<String>,
}

#[node]
impl Sender {
fn new(builder: &mut Builder) -> Self {
Self {
node: builder.node(),
out: builder.output(),
}
}

// When a node starts, its tasks are executed.
#[task]
async fn send(&self) {
self.node.wait(1 * MS).await; // one millisecond of logical time
self.out.set("hello world".to_string()); // send a value through the output port
}
}

#[derive(Ports)]
struct Listener {
// InputPorts receive values. <String> is the type of the values.
inp: InputPort<String>,
}

#[node]
impl Listener {
fn new(builder: &mut Builder) -> Self {
Self {
inp: builder.input(),
}
}

// A reaction runs each time its port receives a value.
#[reaction(inp)]
fn show(&self) {
println!("{}", *self.inp.get().unwrap()); // read input port values with get()
}
}

fn main() {
let mut app = App::new();
let sender = app.create_node(Sender::new);
let listener = app.create_node(Listener::new);
app.connect(&sender.out, &listener.inp); // port connections are type checked
app.run(); // run the nodes until all of them finish
}
$ python hello.py
hello world
$ cargo run
hello world

Add time​

Implement your own timer with await:

timed.py
import raio
from raio import InputPort, Node, OutputPort, S, reaction, task

class TimerSender(Node):
out = OutputPort[str]()

@task
async def send(self):
for count in range(3):
now = await raio.wait(1 * S) # wait one second of logical time
self.out.set(f"tick {count} at {now}")

# Listener is the same as before.
class Listener(Node):
inp = InputPort[str]()

@reaction(inp)
def show(self):
print(self.inp.value)

sender, listener = TimerSender(), Listener()
raio.connect(sender.out, listener.inp)
raio.run(sender, listener)
src/main.rs
use raio::app::{App, Builder, InputPort, Node, OutputPort, Ports, node};
use raio::time::S;

#[derive(Ports)]
struct TimerSender {
node: Node,
out: OutputPort<String>,
}

#[node]
impl TimerSender {
fn new(builder: &mut Builder) -> Self {
Self {
node: builder.node(),
out: builder.output(),
}
}

#[task]
async fn send(&self) {
for count in 0..3 {
let now = self.node.wait(1 * S).await; // wait one second of logical time
self.out.set(format!("tick {count} at {now}"));
}
}
}

// Listener is the same as before.
#[derive(Ports)]
struct Listener {
inp: InputPort<String>,
}

#[node]
impl Listener {
fn new(builder: &mut Builder) -> Self {
Self {
inp: builder.input(),
}
}

#[reaction(inp)]
fn show(&self) {
println!("{}", *self.inp.get().unwrap());
}
}

fn main() {
let mut app = App::new();
let sender = app.create_node(TimerSender::new);
let listener = app.create_node(Listener::new);
app.connect(&sender.out, &listener.inp);
app.run();
}
$ python timed.py
tick 0 at 1s
tick 1 at 2s
tick 2 at 3s
$ cargo run
tick 0 at 1s
tick 1 at 2s
tick 2 at 3s

The timestamps are the same in every run.

To skip the three-second wait, for example in a test:

raio.run(sender, listener, fast_forward=True) # same output, finishes at once

Next steps​