gpt4 book ai didi

go - 为什么这段代码得到 `Error during running of the command error="exec : not started"`?

转载 作者:行者123 更新时间:2023-12-05 04:23:16 26 4
gpt4 key购买 nike

这是我的代码(writeFromProcessToFileWithMax 是一个内部函数,并且工作正常):

    // Go routines for non-blocking reading of stdout and stderr and writing to files
g := new(errgroup.Group)

// Read stdout in goroutine.
g.Go(func() error {
err = writeFromProcessToFileWithMax(stdoutScanner, stdoutFileWriter, maxStdoutFileLengthInGB)
if err != nil {
log.Error().Err(err).Msgf("Error writing to stdout file: %s", stdoutFilename)
return err
}
return nil
})

// Read stderr in goroutine.
g.Go(func() error {
err = writeFromProcessToFileWithMax(stderrScanner, stderrFileWriter, maxStderrFileLengthInGB)
if err != nil {
log.Error().Err(err).Msgf("Error writing to stderr file: %s", stderrFilename)
return err
}
return nil
})

// Wait the command in a goroutine.
g.Go(func() error {
return cmd.Wait()
})

// Starting the command
if err = cmd.Start(); err != nil {
log.Error().Err(err).Msg("Error starting command")
return err
}

// Waiting until errorGroups groups are done
if err = g.Wait(); err != nil {
log.Error().Err(err).Msg("Error during running of the command")
}

当我运行它时,出现以下错误 = 运行命令时出错 error="exec: not started"。但一切正常。

它会回来咬我还是我应该压制?

最佳答案

在启动它之前,您正在等待 cmdcmd.Wait() 将在 cmd.Start() 大多数情况下 在您的旧代码中被调用。 (无法保证两个不同的 goroutine 中的事情何时发生,除非您明确使用同步点)

交换cmd.Start()cmd.Wait()在goroutine中的顺序:

// Starting the command
if err = cmd.Start(); err != nil {
log.Error().Err(err).Msg("Error starting command")
return err
}

// Wait the command in a goroutine.
g.Go(func() error {
return cmd.Wait()
})

当您启动启动命令后等待的 goroutine 时,您保证执行 cmd.Start()cmd.Wait() 顺序正确。

至于为什么它似乎有效:g.Wait() "blocks until all function calls from the Go method have returned, then returns the first non-nil error (if any) from them. "

因此所有 go 例程都已完成,包括复制输出的例程,然后您会看到执行 cmd.Wait() 的错误。

关于go - 为什么这段代码得到 `Error during running of the command error="exec : not started"`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73751161/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com