Concurrency
Use case: CPU work on a worker thread
Section titled “Use case: CPU work on a worker thread”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 }}vow run tests/lang/threads_sum.vow -- --grant threadsWorkers are named fn() -> int today — lambdas are not spawnable in v1.
Use case: run an external process
Section titled “Use case: run an external process”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 }}vow run tests/lang/process_echo.vow -- --grant proc:Use case: async / await (0.1.0)
Section titled “Use case: async / await (0.1.0)”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);}bash tests/lang/test_async_general.sh # exit 49bash tests/lang/test_async_db.shHTTP 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 |
- Async / await — full guide
- HTTP API
- vow-postgres
- Standard library
- Examples gallery