【发布时间】:2018-02-08 13:51:43
【问题描述】:
我有一个程序通过cmd := exec.Command(name, args…) 执行一些命令
现在程序正在为 10 个(或更多)不同的目录执行此操作,
我的意思是我为给定路径的 10 个不同目录运行 npm install。
这个想法是 wait 执行过程将 结束 然后压缩整个文件夹(运行命令 npm install 的位置)
这里的问题是,当wait 块正在执行时,程序停止并等待(当然应该这样做......)但我想进入下一个目录并且不要等待到wait(每个目录执行)完成
建议如何以充分的方式处理它? 我的意思是在执行模块和 此外,当特定命令完成后在指定目录上运行时,无论程序何时处于进程中,都会自动对其进行压缩
这个函数针对不同的目录循环调用10次
func (n Node) Build(path string) error {
//e.g. Run npm install which build's nodejs project
command := exec.Command("npm", "install")
command.Dir = n.path
//start the execution
if err := command.Start(); err != nil {
log.Printf("Failed to start cmd: %v", err)
}
// Here I waiting to command to finish to zip the folder
if err := command.Wait(); err != nil {
// Zip folder
}
}
主函数如下调用它
func main() {
//Here I have loop which
for _, dir := range dirs {
switch dir.name {
case "Java":
Build(&Java{path})
case "Go":
Build(&Golang{path,cgoOptions},)
case "Node":
Build(&Node{path})
}
}
【问题讨论】:
-
性能?如果您的文件很大,避免复制将是一大优势。
-
您有两个选择,并且您想知道哪个表现更好,所以尝试每一个,衡量每个的性能,然后比较它们。
标签: go