Last active
July 28, 2021 21:04
-
-
Save audstanley/417c7e18616664209f86c9bab91da729 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"io" | |
"log" | |
"os" | |
"os/exec" | |
"sync" | |
"time" | |
) | |
func runThing(c1 chan string, wg *sync.WaitGroup) { | |
defer wg.Done() | |
cmd := exec.Command("./subprocess.sh") | |
cmd.Stderr = os.Stderr | |
stdout, err := cmd.StdoutPipe() | |
if nil != err { | |
log.Fatalf("Error obtaining stdout: %s", err.Error()) | |
} | |
reader := bufio.NewReader(stdout) | |
go func(reader io.Reader) { | |
scanner := bufio.NewScanner(reader) | |
for scanner.Scan() { | |
log.Printf("running subprocess: %s", scanner.Text()) | |
c1 <- scanner.Text() | |
} | |
}(reader) | |
if err := cmd.Start(); nil != err { | |
log.Fatalf("Error starting program: %s, %s", cmd.Path, err.Error()) | |
} | |
fmt.Println("we are near the end") | |
cmd.Wait() | |
fmt.Println("we are done") | |
} | |
func channelListener(c1 <- chan string) { | |
for { | |
select { | |
case msg := <-c1: { | |
fmt.Println("message:", msg) | |
} | |
default: { | |
time.Sleep(time.Millisecond*100) | |
} | |
} | |
} | |
} | |
func main() { | |
var wg sync.WaitGroup | |
wg.Add(2) | |
c1 := make(chan string) | |
go runThing(c1, &wg) | |
go runThing(c1, &wg) | |
go channelListener(c1) | |
wg.Wait() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
echo "blah blah blah" | |
sleep 10 | |
echo "sub done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment