v2 / vlib / os / util / util.v
24 lines · 22 sloc · 453 bytes · 652881c73b95efd963aaca9d49116388e132950a
Raw
1module util
2
3import os
4
5// TODO `select` doesn't work with time.Duration for some reason
6pub fn execute_with_timeout(cmd string, timeout i64) ?os.Result {
7 ch := chan os.Result{cap: 1}
8 spawn fn [cmd] (c chan os.Result) {
9 res := os.execute(cmd)
10 c <- res
11 }(ch)
12 select {
13 a := <-ch {
14 return a
15 }
16 // timeout {
17 // 1000 * time.millisecond {
18 // timeout * time.millisecond {
19 timeout * 1_000_000 {
20 return none
21 }
22 }
23 return os.Result{}
24}
25