cfCmd := exec.Command("perl", "./test.pl")
cfIn, _ := cfCmd.StdinPipe()
cfOut, _ := cfCmd.StdoutPipe()
err := cfCmd.Start()
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
}
cfIn.Write([]byte(task))
cfIn.Close()
cfBytes, _ := ioutil.ReadAll(cfOut)
cfCmd.Wait()
res := "\n>>> Server: test.pl " + fmt.Sprint(id) + " вернул: \n" + string(cfBytes)
package main
import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"strings"
)
func main() {
cfCmd := exec.Command("perl", "test.pl")
cfIn, err := cfCmd.StdinPipe()
if err != nil {
panic(err)
}
cfOut, err := cfCmd.StdoutPipe()
if err != nil {
panic(err)
}
err = cfCmd.Start()
if err != nil {
panic(err)
}
go ProcessStdin(cfIn)
ProcessStdout(cfOut)
err = cfCmd.Wait()
if err != nil {
panic(err)
}
}
func ProcessStdin(stdin io.WriteCloser) {
defer stdin.Close()
stdin.Write([]byte("some task"))
stdin.Write([]byte("some other task"))
// ......
}
func ProcessStdout(stdout io.ReadCloser) {
defer stdout.Close()
rd := bufio.NewReader(stdout)
for {
line, err := rd.ReadString('\n')
if err != nil {
fmt.Println("Error reading stdout:", err)
os.Exit(1)
}
line = strings.TrimSpace(line)
fmt.Println("Got line from a process:", line)
}
}
$| = 1;