@@ -12,12 +12,11 @@ A helper function for reading all of the bytes from a handle.
1212
1313``` ocaml
1414let read_all handle buf =
15- let rec read acc =
16- try
17- let i = Eio_luv.Low_level.Stream.read_into handle buf in
18- read (acc + i)
19- with End_of_file -> acc
20- in read 0
15+ let rec read acc =
16+ match Eio_luv.Low_level.Stream.read_into handle buf with
17+ | i -> read (acc + i)
18+ | exception End_of_file -> acc
19+ in read 0
2120```
2221
2322A simple ` echo hello ` process redirects to stdout.
@@ -42,7 +41,7 @@ Using a pipe to redirect output to a buffer.
4241 let parent_pipe = Eio_luv.Low_level.Pipe.init ~sw () in
4342 let buf = Luv.Buffer.create 32 in
4443 let redirect = Eio_luv.Low_level.Process.[
45- to_parent_pipe ~fd:Luv.Process.stdout ~parent_pipe:parent_pipe ()
44+ to_parent_pipe ~fd:Luv.Process.stdout ~parent_pipe ()
4645 ] in
4746 let t = Process.spawn ~sw ~redirect "echo" [ "echo"; "Hello,"; "World!" ] in
4847 let read = read_all parent_pipe buf in
@@ -88,35 +87,30 @@ Forgetting to wait for a process to finish stops the process.
8887
8988``` ocaml
9089# Eio_luv.run @@ fun _env ->
91- let proc = ref None in
92- let run () =
90+ let proc =
9391 Switch.run @@ fun sw ->
9492 let redirect = Luv.Process.[
9593 inherit_fd ~fd:stdout ~from_parent_fd:stdout ()
9694 ] in
97- let t = Process.spawn ~sw ~redirect "sleep" [ "sleep"; "10" ] in
98- proc := Some t
95+ Process.spawn ~sw ~redirect "sleep" [ "sleep"; "10" ]
9996 in
100- run ();
101- Process.await_exit (Option.get !proc);;
97+ Process.await_exit proc;;
10298- : int * int64 = (9, 0L)
10399```
104100
105101Stopping a process interacts nicely with switches.
106102
107103``` ocaml
108104# Eio_luv.run @@ fun _env ->
109- let proc = ref None in
110- let run () =
105+ let proc =
111106 Switch.run @@ fun sw ->
112107 let redirect = Luv.Process.[
113108 inherit_fd ~fd:stdout ~from_parent_fd:stdout ()
114109 ] in
115110 let t = Process.spawn ~sw ~redirect "sleep" [ "sleep"; "10" ] in
116111 Process.send_signal t Luv.Signal.sigkill;
117- proc := Some t
112+ t
118113 in
119- run ();
120- Process.await_exit (Option.get !proc);;
114+ Process.await_exit proc;;
121115- : int * int64 = (9, 0L)
122116```
0 commit comments