我正在尝试制作一个代码来从文件夹链接扫描我的所有文件并制作一个"前10"根据他的内容和他的名字,他的大小也是一个正则表达式。文件。根据它的内容,我用goroutines创建频道,但我不明白为什么每次我的goroutines被锁定。这是我的代码:
package main
import (
"flag"
"fmt"
"io/ioutil"
"regexp"
"runtime"
"sort"
"sync"
"time"
)
var rName = ".php"
var rContent = "php"
var maxSize, minSize int64
var files_ten []File
func main() {
start := time.Now()
channelOne := make(chan File)
channelTwo := make(chan File)
var wg sync.WaitGroup
var path string
flag.StringVar(&path, "path", "", "Path to folder")
flag.Parse()
fmt.Println("Path=", path)
for i := 0; i < runtime.NumCPU(); i++ {
go check(channelOne, channelTwo, &wg)
}
go top10(channelTwo, &wg)
wg.Wait()
getFolder(path, channelOne, &wg)
fmt.Println("top 10", files_ten)
t := time.Now()
current := t.Sub(start)
fmt.Println(current)
}
type File struct {
Size int64
Name string
Path string
}
func (this File) GetSize() int64 {
return this.Size
}
func getFolder(path string, channelOne chan File, wg *sync.WaitGroup) {
folder, err := ioutil.ReadDir(path)
if err != nil {
fmt.Println("Error:", err)
return
}
for _, data := range folder {
if data.IsDir() {
var newFolder string = path + data.Name() + "/"
getFolder(newFolder, channelOne, wg)
} else {
wg.Add(1)
channelOne <- File{Size: data.Size(), Name: data.Name(), Path: path}
}
}
}
func check(channelOne chan File, channelTwo chan File, wg *sync.WaitGroup) {
for {
file := <-channelOne
rName := regexp.MustCompile(rName)
maxSize = 10000
minSize = 0
if rName.MatchString(file.Name) {
if file.Size <= maxSize && file.Size >= minSize {
f, err := ioutil.ReadFile(file.Path + "/" + file.Name)
if err != nil {
fmt.Println("Error:", err)
return
}
rContent := regexp.MustCompile(rContent)
if rContent.MatchString(string(f)) {
channelTwo <- file
} else {
wg.Done()
}
} else {
wg.Done()
}
} else {
wg.Done()
}
}
}
func sortFilesFromBiggestToLowerSize(arrayFile []File) []File {
sort.Slice(arrayFile, func(i, j int) bool {
return arrayFile[i].Size > arrayFile[j].Size
})
return arrayFile
}
func top10(channelTwo chan File, wg *sync.WaitGroup) []File {
for {
f := <-channelTwo
if len(files_ten) == 10 {
if f.Size > files_ten[0].Size || f.Size >
files_ten[len(files_ten)-1].Size {
files_ten = files_ten[:len(files_ten)-1]
files_ten = append(files_ten, f)
return sortFilesFromBiggestToLowerSize(files_ten)
}
} else {
sortFilesFromBiggestToLowerSize(files_ten)
return append(files_ten, f)
}
wg.Done()
return files_ten
}
}
每次编译时都出现错误:
go run filebysize.go --path=C:/wamp64/www/symfony/init/cours1/
Path= C:/wamp64/www/symfony/init/cours1/
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan send]:
main.getFolder(0xc04210a3c0, 0x3d, 0xc04204c0c0, 0xc04204e210)
C:/Users/Sahra/Documents/go/display/filebysize.go:72 +0x28a
main.getFolder(0xc04210a200, 0x32, 0xc04204c0c0, 0xc04204e210)
C:/Users/Sahra/Documents/go/display/filebysize.go:69 +0x151
main.getFolder(0xc04200e6c0, 0x26, 0xc04204c0c0, 0xc04204e210)
C:/Users/Sahra/Documents/go/display/filebysize.go:69 +0x151
main.getFolder(0xc042051f57, 0x22, 0xc04204c0c0, 0xc04204e210)
C:/Users/Sahra/Documents/go/display/filebysize.go:69 +0x151
main.main()
C:/Users/Sahra/Documents/go/display/filebysize.go:37 +0x2e0
goroutine 19 [chan send]:
main.check(0xc04204c0c0, 0xc04204c120, 0xc04204e210)
C:/Users/Sahra/Documents/go/display/filebysize.go:95 +0x2ec
created by main.main
C:/Users/Sahra/Documents/go/display/filebysize.go:32 +0x26d
goroutine 20 [chan send]:
main.check(0xc04204c0c0, 0xc04204c120, 0xc04204e210)
C:/Users/Sahra/Documents/go/display/filebysize.go:95 +0x2ec
created by main.main
C:/Users/Sahra/Documents/go/display/filebysize.go:32 +0x26d
goroutine 21 [chan send]:
main.check(0xc04204c0c0, 0xc04204c120, 0xc04204e210)
C:/Users/Sahra/Documents/go/display/filebysize.go:95 +0x2ec
created by main.main
C:/Users/Sahra/Documents/go/display/filebysize.go:32 +0x26d
goroutine 22 [chan send]:
main.check(0xc04204c0c0, 0xc04204c120, 0xc04204e210)
C:/Users/Sahra/Documents/go/display/filebysize.go:95 +0x2ec
created by main.main
C:/Users/Sahra/Documents/go/display/filebysize.go:32 +0x26d
goroutine 23 [chan send]:
main.check(0xc04204c0c0, 0xc04204c120, 0xc04204e210)
C:/Users/Sahra/Documents/go/display/filebysize.go:95 +0x2ec
created by main.main
C:/Users/Sahra/Documents/go/display/filebysize.go:32 +0x26d
goroutine 24 [chan send]:
main.check(0xc04204c0c0, 0xc04204c120, 0xc04204e210)
C:/Users/Sahra/Documents/go/display/filebysize.go:95 +0x2ec
created by main.main
C:/Users/Sahra/Documents/go/display/filebysize.go:32 +0x26d
goroutine 25 [chan send]:
main.check(0xc04204c0c0, 0xc04204c120, 0xc04204e210)
C:/Users/Sahra/Documents/go/display/filebysize.go:95 +0x2ec
created by main.main
C:/Users/Sahra/Documents/go/display/filebysize.go:32 +0x26d
goroutine 26 [chan send]:
main.check(0xc04204c0c0, 0xc04204c120, 0xc04204e210)
C:/Users/Sahra/Documents/go/display/filebysize.go:95 +0x2ec
created by main.main
C:/Users/Sahra/Documents/go/display/filebysize.go:32 +0x26d
exit status 2
答案 0 :(得分:1)
你试图发送channelOne
,但是在wg.Done之后没有任何内容读取,因此死锁:试图发送给它的例程必须等到有东西可以接收,这种情况永远不会发生
此外,您的WaitGroup使用已关闭;你应该在开始你想要等待的每个goroutine之前调用Add
,然后在goroutine结束时调用Done
。单个goroutine不应在循环中调用Add
或Done
,如果没有关联的Done
调用,则goroutine不应调用Add
。
看起来你有多个永远不会退出的for
循环;他们没有条件,也没有break
s。
您还可以更简单地循环通道。您可以替换以下结构:
for {
file := <-channelOne
更简单:
for file := range channelOne {
这样做的另一个好处是,当您关注的频道关闭时,循环将退出,允许您使用关闭频道作为消费者可以停止的信号。