Created
November 26, 2012 13:22
-
-
Save royguo/4148170 to your computer and use it in GitHub Desktop.
go_dead_lock
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 ( | |
"fmt" | |
"os" | |
"bufio" | |
"sync" | |
) | |
// Load data into channel | |
func laodData(arr []string,channel chan string,wg sync.WaitGroup) { | |
for _,path := range arr { | |
file,err := os.Open(path) | |
fmt.Println("begin to laodData ", path) | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(-1) | |
} | |
defer file.Close() | |
reader := bufio.NewReaderSize(file, 32*10*1024) | |
i := 0 | |
for { | |
line,err := reader.ReadString('\n') | |
channel <- line | |
if err != nil { | |
break | |
} | |
i++ | |
if i%200 == 0 { | |
fmt.Println(i," lines parsed") | |
} | |
} | |
fmt.Println("finish laodData ", path) | |
} | |
wg.Done() | |
} | |
// dispatch data lines into different mappers | |
func dispatcher(channel chan string,wg sync.WaitGroup){ | |
fmt.Println("pull data 11") | |
line,ok := <- channel | |
fmt.Println(ok) | |
for ok { | |
fmt.Println(line) | |
line,ok = <- channel | |
} | |
fmt.Println("pull data 22") | |
wg.Done() | |
} | |
func main() { | |
path := os.Args | |
if len(path) < 2 { | |
fmt.Println("Need Input Files") | |
os.Exit(0) | |
} | |
var wg sync.WaitGroup | |
wg.Add(2) | |
channel := make(chan string) | |
defer close(channel) | |
fmt.Println("before dispatcher") | |
go laodData(path[1:],channel,wg) | |
go dispatcher(channel,wg) | |
wg.Wait() | |
fmt.Println("after dispatcher") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment