feat: contexts support

This commit is contained in:
ge
2026-01-03 19:17:50 +03:00
parent b790cfef0a
commit 055dab663e
5 changed files with 176 additions and 9 deletions

View File

@@ -0,0 +1,27 @@
import context
import runcmd
import time
fn main() {
// Create context with cancel.
mut bg := context.background()
mut ctx, _ := context.with_timeout(mut bg, 10 * time.second)
// Create new command with context.
mut cmd := runcmd.with_context(ctx, 'sleep', '120')
// Start a command.
started := time.now()
println('Start command at ${started}')
cmd.start()!
// Wait for command.
cmd.wait()!
// The `sleep 120` command would run for two minutes without a timeout.
// But in this example, it will time out after 10 seconds.
println('Command finished after ${time.now() - started}')
// Since command has been terminated, the state would be: `signal: 15 (SIGTERM)`
println('Child state: ${cmd.state}')
}