Futures & Coroutines
Pyle’s concurrency is cooperative and single-threaded. Coroutines yield explicitly, and futures signal completion.
Coro(fn) wraps a function in a coroutine.
let gen = Coro(fn() { let i = 0 while i < 3 { yield i i += 1 }})
print(gen.resume()) # 0print(gen.resume()) # 1print(gen.resume()) # 2print(gen.resume()) # none, the coroutine is dead| Method | Returns | Description |
|---|---|---|
resume(arg) |
any |
Run until the next yield or return |
state() |
string |
"suspended", "running", or "dead" |
error() |
PCallError? |
none, or an error map if the coroutine died from a panic |
A value passed to resume becomes the result of the yield expression inside the coroutine.
let c = Coro(fn() { let received = yield "ready" print(received)})
print(c.resume()) # readyc.resume("hello") # helloIf the coroutine body panics, c.state() becomes "dead" and c.error() returns the error map.
Future
Section titled “Future”Future() creates an unresolved handle.
let f = Future()
f.resolve(42)print(f.is_done) # trueprint(f.has_failed) # falseprint(f.data) # 42| Member | Returns | Description |
|---|---|---|
resolve(value) |
none |
Complete successfully |
reject(message) |
none |
Complete with an error string |
set_cancel_hook(callback) |
none |
Register what cancel() should do |
cancel() |
bool |
Best-effort cancel |
is_done |
bool |
Read-only |
has_failed |
bool |
Read-only |
is_cancelled |
bool |
Read-only |
data |
any |
Resolved value |
error |
string |
Error string when rejected |
let f = Future()f.reject("something went wrong")
print(f.has_failed) # trueprint(f.error) # something went wronglet f = Future()f.set_cancel_hook(fn() { print("cancelled")})
print(f.cancel()) # trueprint(f.is_cancelled) # truecancel() returns false when the future has no cancel hook or is already done. Native producers such as http.get_async install their own hooks, so cancelling the future aborts the underlying operation.
waitfor
Section titled “waitfor”waitfor(future) suspends the current coroutine until the future completes. If the future was rejected, it raises FutureRejected.
fn main() { let result = waitfor(os.sleep_async(1000)) print("done")}
async.run(main)A function that calls waitfor must run inside async.run, async.all, or a coroutine that has a resumer. Calling it at top level with no coroutine produces an error.
async.run
Section titled “async.run”async.run(task) drives a function or coroutine to completion and returns its result.
fn task() { waitfor(os.sleep_async(100)) return "finished"}
print(async.run(task)) # finishedA failed task’s trace prints once at the escalation point when async.run re-raises.
async.all
Section titled “async.all”async.all(tasks) runs tasks concurrently and returns an array of outcomes.
fn task_a() { waitfor(os.sleep_async(1000)) return "a"}
fn task_b() { waitfor(os.sleep_async(1000)) return "b"}
let results = async.all([task_a, task_b])
for result in results { if result.ok { print(result.value) } else { print(result.error) }}The two tasks overlap, so the total wait is about one second rather than two. Each result is {ok: true, value} or {ok: false, error}.
async.all collects failures without printing them. The failure’s trace prints once at the escalation point when async.run re-raises or waitfor raises FutureRejected.
Async producers in the core
Section titled “Async producers in the core”| Producer | Returns |
|---|---|
os.sleep_async(ms) |
Future resolving after ms |
File.read_all_async() and friends |
Future resolving to the result |
proc.run_async(...) |
Future resolving to a process result |
Worker.recv_async() |
Future resolving to a string |
http.*_async(...) |
Future resolving to a response |
On wasm32, sleep and file async operations resolve synchronously because there are no threads.
Practical patterns
Section titled “Practical patterns”Bounded work
Section titled “Bounded work”fn worker(id) { waitfor(os.sleep_async(10)) return id * 2}
let tasks = [fn() { worker(1) }, fn() { worker(2) }, fn() { worker(3) }]let results = async.all(tasks)Cancellation
Section titled “Cancellation”let f = http.get_async(url)if not f.cancel() { print("already finished")}Manual driving
Section titled “Manual driving”let c = Coro(fn() { while true { let cmd = yield if cmd == "stop" { return } print(cmd) }})
c.resume()c.resume("hello")c.resume("stop")print(c.state()) # deadRelated
Section titled “Related”- Coroutines & Async for the language guide.
- os for
sleep_asyncand workers. - Files for async file operations.