英文:
Go exec.Command(...).Wait() hangs for ever
问题
大家好,由于某种原因,当我执行mysql命令时,Wait()函数会永远挂起,有人知道为什么吗?
以下是我的代码:
// 将数据导入本地数据库
func (x MySQL) Import(data string, opt LocalDb) {
var stderr bytes.Buffer
cmd := exec.Command("mysql", x.importOptions(opt)...)
// 设置<管道变量
stdin, err := cmd.StdinPipe()
errChk(err)
cmd.Stderr = &stderr
cmd.Start()
// 将数据写入管道
io.WriteString(stdin, data)
fmt.Println("正在导入" + x.DB + "到本地...")
// 记录mysql错误
if err := cmd.Wait(); err != nil {
log.Fatal(stderr.String())
} else {
fmt.Println("导入完成")
}
}
这个函数完成了所有操作,mysql将数据导入数据库,但它永远不会从Wait()函数中返回,即使已经完成。
英文:
Hi guys for some reason Wait() when I execute mysql command hangs for ever, does anyone know why?
Here is my code.
// Import imports data into Local database
func (x MySQL) Import(data string, opt LocalDb) {
var stderr bytes.Buffer
cmd := exec.Command("mysql", x.importOptions(opt)...)
// Set < pipe variable
stdin, err := cmd.StdinPipe()
errChk(err)
cmd.Stderr = &stderr
cmd.Start()
// Write data to pipe
io.WriteString(stdin, data)
fmt.Println("Importing " + x.DB + " to localhost...")
// Log mysql error
if err := cmd.Wait(); err != nil {
log.Fatal(stderr.String())
} else {
fmt.Println("Importing complete")
}
}
This function accomplishes everything and mysql imports the data into database but it never return from Wait() just freezes there even though is completed.
答案1
得分: 3
问题在于您没有关闭stdin
管道。MySQL将保持活动状态,直到关闭该管道。
修复方法很简单:
// 将数据写入管道
io.WriteString(stdin, data)
stdin.Close()
fmt.Println("将" + x.DB + "导入到本地主机...")
StdinPipe()
的行为如下所述:
StdinPipe
返回一个管道,该管道在命令启动时将连接到命令的标准输入。*在Wait
看到命令退出后,该管道将自动关闭。调用者只需调用Close
来更早地强制关闭管道。*例如,如果运行的命令直到标准输入关闭后才退出,则调用者必须关闭该管道。
英文:
The problem is that you haven't closed the stdin
pipe. MySQL will remain active until it is.
The fix is thus simple:
// Write data to pipe
io.WriteString(stdin, data)
stdin.Close()
fmt.Println("Importing " + x.DB + " to localhost...")
The fact that StdinPipe()
acts in this way is documented as such:
> StdinPipe returns a pipe that will be connected to the command's standard input when the command starts. The pipe will be closed automatically after Wait sees the command exit. A caller need only call Close to force the pipe to close sooner. For example, if the command being run will not exit until standard input is closed, the caller must close the pipe.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论