-
-
Save adam-stokes/f7ec0c1c34e69436f0759e7a75643cd2 to your computer and use it in GitHub Desktop.
Golang Commands in Goroutines
This file contains hidden or 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 ( | |
"fmt" | |
"log" | |
"os/exec" | |
"runtime" | |
) | |
type Worker struct { | |
Command string | |
Args string | |
Output chan string | |
} | |
func (cmd *Worker) Run() { | |
out, err := exec.Command(cmd.Command, cmd.Args).Output() | |
if err != nil { | |
log.Fatal(err) | |
} | |
cmd.Output <- string(out) | |
} | |
func Collect(c chan string) { | |
for { | |
msg := <-c | |
fmt.Printf("The command result is %s\n", msg) | |
} | |
} | |
func main() { | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
var read string | |
fmt.Println("When you're ready press ENTER to spawn goroutine") | |
fmt.Scanln(&read) | |
c := make(chan string) | |
phpService := &Worker{Command: "php", Args: "slowService.php", Output: c} | |
pythonService := &Worker{Command: "python", Args: "mediumService.py", Output: c} | |
go phpService.Run() | |
fmt.Println("Doing something...") | |
go pythonService.Run() | |
go Collect(c) | |
fmt.Scanln(&read) | |
} |
This file contains hidden or 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
import time | |
time.sleep(1) | |
print "Python returns!" |
This file contains hidden or 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
<?php | |
sleep(3); | |
print "PHP returns!"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment