Node.js child_process-like subprocess management for MoonBit
Dependencies
// Shell command execution
let result = @subprocess.exec("ls -la")
println(result.stdout)
// Direct file execution
let result = @subprocess.exec_file("git", args=["status"])
println(result.stdout)
// Spawn with streaming I/O
@async.with_task_group(async fn(group) {
let child = @subprocess.spawn(group, "cat", pipe_stdin=true, pipe_stdout=true)
child.stdin.unwrap().write("hello")
child.stdin.unwrap().close()
let output = child.stdout.unwrap().read_all()
println(output.text())
let _ = child.wait()
group.return_immediately(())
})
// Managed process pool (zombie prevention)
let pm = @managed.ProcessManager::new()
let r = pm.exec("echo hello")
pm.shutdown() // cancels all, waits, no zombies
// IPC socket
let server = @socket.Server::new("/tmp/my_app.sock")
let client = @socket.Connection::connect("/tmp/my_app.sock")
let conn = server.accept()
client.send("hello") // length-prefixed message
let msg = conn.recv() // => Some("hello")
client.close()
conn.close()
server.close()| Function | Description |
|---|---|
| exec(command, cwd?, env?, inherit_env?, check?) | Run shell command, return ExecResult |
| exec_file(file, args?, cwd?, env?, inherit_env?, check?) | Run executable, return ExecResult |
| spawn(group, command, args?, ..., pipe_stdin?, pipe_stdout?, pipe_stderr?) | Spawn with streaming I/O |
| spawn_shell(group, command, ..., pipe_stdin?, pipe_stdout?, pipe_stderr?) | Spawn shell command with streaming I/O |
| Function | Description |
|---|---|
| ProcessManager::new() | Create empty manager |
| pm.exec(...) / pm.exec_file(...) | Execute through manager |
| pm.spawn(...) / pm.spawn_shell(...) | Spawn tracked process |
| pm.reap() | Clean up completed processes |
| pm.active_count() | Count running processes |
| pm.cancel_all() | Cancel all running processes |
| pm.shutdown() | Cancel + wait all (zombie-safe) |
| pm.wait_all() | Wait for all without cancelling |
| Function | Description |
|---|---|
| Server::new(path) | Create IPC socket server |
| server.accept() | Accept connection (blocking) |
| server.poll_accept(timeout_ms?) | Non-blocking accept with timeout |
| server.set_nonblocking() | Set server to non-blocking mode |
| server.close() | Close server and remove socket file |
| Connection::connect(path) | Connect to an IPC socket server |
| conn.send(message) | Send length-prefixed UTF-8 message |
| conn.recv() | Receive length-prefixed message (None on EOF) |
| conn.write_bytes(data) | Send raw bytes |
| conn.read_bytes(max_len?) | Read raw bytes |
| conn.peer_pid() | Get peer process PID (macOS/Linux) |
| conn.poll(timeout_ms?) | Check for data readiness |
| conn.close() | Close connection |
| Function | Description |
|---|---|
| IpcChannel::from_socket(conn) | Channel over IPC socket |
| IpcChannel::from_child(child) | Channel over subprocess pipes |
| channel.send(msg) / channel.recv() | Unified messaging |
pub struct ChildProcess {
process : Process
stdin : WriteToProcess?
stdout : ReadFromProcess?
stderr : ReadFromProcess?
}pub(all) struct ExecResult {
exit_code : Int
stdout : String
stderr : String
}impl Show for ExecResultasync fn exec(command : String, cwd? : String, env? : Map[String, String], inherit_env? : Bool, check? : Bool) -> ExecResult raise SubprocessErrorasync fn exec_file(file : String, args? : Array[String], cwd? : String, env? : Map[String, String], inherit_env? : Bool, check? : Bool) -> ExecResult raise SubprocessErrorasync fn[X] spawn(group : TaskGroup[X], command : String, args? : Array[String], cwd? : String, env? : Map[String, String], inherit_env? : Bool, pipe_stdin? : Bool, pipe_stdout? : Bool, pipe_stderr? : Bool, cancel_handler? : CancellationHandler) -> ChildProcess raise SubprocessErrorasync fn[X] spawn_shell(group : TaskGroup[X], command : String, cwd? : String, env? : Map[String, String], inherit_env? : Bool, pipe_stdin? : Bool, pipe_stdout? : Bool, pipe_stderr? : Bool, cancel_handler? : CancellationHandler) -> ChildProcess raise SubprocessErrorNode.js child_process-like subprocess management for MoonBit
Dependencies