Skip to content

Concurrency

fn worker_a() -> int {
var n = 0
var i = 0
while i < 5000 {
n = n + i
i = i + 1
}
return n
}
fn main(caps: Caps) -> int {
return match caps.threads {
Some(th) => with th {
match thread_spawn(worker_a) {
Ok(t) => match thread_join(t) {
Ok(n) => n,
Err(e) => e
},
Err(e) => e
}
},
None => 1
}
}
Terminal window
vow run tests/lang/threads_sum.vow -- --grant threads

Workers are named fn() -> int today — lambdas are not spawnable in v1.

fn echo_once(p: Proc) -> int needs Proc {
let args = str_split("hello", "|")
return match process_spawn(p, "/bin/echo", args) {
Ok(child) => match process_wait(p, child) {
Ok(code) => code,
Err(e) => e
},
Err(e) => e
}
}
fn main(caps: Caps) -> int {
return match caps.proc {
Some(p) => with p { echo_once(p) },
None => 1
}
}
Terminal window
vow run tests/lang/process_echo.vow -- --grant proc:

Use async fn for I/O and blocking work. Call from sync code (including HTTP handlers) with block_on.

async fn load() -> int {
let a = await helper();
let b = await spawn_blocking(heavy_sync_fn);
return a + b;
}
async fn query_db(h: int64) -> int {
let rows = await db_query(h, "SELECT id FROM todos")?;
return len(rows);
}
fn main(_caps: Caps) -> int {
return block_on(load);
}
Terminal window
bash tests/lang/test_async_general.sh # exit 49
bash tests/lang/test_async_db.sh

HTTP apps: handlers stay fn(Request) -> Response. Put async logic in helpers:

fn list_todos(_req: request.Request) -> response.Response {
let rows = block_on(list_todos_rows);
return vws.json("{\"todos\":" + rows + "}");
}
async fn list_todos_rows() -> String {
return await db_query(g_handle, "SELECT id, title FROM todos")?;
}

Full walkthrough: Async / await · demo: hello-app/

Await target Notes
await db_query(h, sql)? Postgres / DB cap — thread-pool offload
await read_file_async(path) Needs FsRead
await async_sleep(ms) Needs Clock
await spawn_blocking(fn) No-arg fn() -> int
await other_async_fn() No-arg async composition