在Go中,如果结束则程序将停止main
方法(在main
包)已到达。 Go 语言规范中描述了此行为程序执行部分 https://golang.org/ref/spec#Program_execution(强调我自己的):
程序执行开始于初始化main
封装然后调用函数main
。当该函数调用返回时,程序退出。它不会等待其他(非主)goroutines 完成。
Defects
我将考虑您的每个示例及其相关的控制流缺陷。您将在下面找到 Go Playground 的链接,但这些示例中的代码不会在限制性 Playground 沙箱中执行,因为sleep
找不到可执行文件。复制并粘贴到您自己的环境中进行测试。
多个 Goroutine 示例
case <-time.After(3 * time.Second):
log.Println("closing via ctx")
ch <- struct{}{}
在计时器触发并且您向 goroutine 发出信号后,是时候杀死孩子并停止工作了,没有什么会导致main
方法阻止并等待此完成,因此它返回。根据语言规范,程序退出。
调度程序可能会在通道传输后触发,因此之间可能存在竞争main
退出并且其他 goroutine 醒来接收来自ch
。然而,假设任何特定的行为交错是不安全的——而且,出于实际目的,之前不太可能发生任何有用的工作main
退出。这sleep
子进程将是orphaned https://en.wikipedia.org/wiki/Orphan_process;在 Unix 系统上,操作系统通常会将进程重新定位到init
过程。
单个 goroutine 示例
在这里,你遇到了相反的问题:main
不返回,因此子进程不会被终止。这种情况只有在子进程退出时(5分钟后)才能解决。出现这种情况是因为:
- 致电给
cmd.Wait
in the Run
方法是一个阻塞调用(docs https://golang.org/pkg/os/exec/#Cmd.Wait). The select
语句被阻塞等待cmd.Wait
返回错误值,因此无法从quit
渠道。
-
The quit
通道(声明为ch
in main
) is an 无缓冲通道。无缓冲通道上的发送操作将阻塞,直到接收器准备好接收数据。来自频道语言规范 https://golang.org/ref/spec#Channel_types(再次强调我自己的):
容量(以元素数量表示)设置通道中缓冲区的大小。如果容量为零或不存在,则通道无缓冲并且只有当发送方和接收方都准备好时,通信才能成功.
As Run
被封锁在cmd.Wait
,没有准备好的接收器来接收通道上传输的值ch <- struct{}{}
中的声明main
方法。main
阻止等待传输此数据,从而阻止进程返回。
我们可以通过小的代码调整来演示这两个问题。
cmd.Wait
正在阻塞
暴露阻塞本质cmd.Wait
,声明以下函数并用它代替Wait
称呼。该函数是一个包装器,其行为与cmd.Wait
,但是打印 STDOUT 发生的情况会产生额外的副作用。 (游乐场链接 https://play.golang.org/p/BTN9i_9oW9d):
func waitOn(cmd *exec.Cmd) error {
fmt.Printf("Waiting on command %p\n", cmd)
err := cmd.Wait()
fmt.Printf("Returning from waitOn %p\n", cmd)
return err
}
// Change the select statement call to cmd.Wait to use the wrapper
case e <- waitOn(cmd):
运行这个修改后的程序后,您将观察到输出Waiting on command <pointer>
到控制台。定时器触发后,您将观察输出calling ctx cancel
,但没有对应的Returning from waitOn <pointer>
文本。这种情况只会在子进程返回时发生,您可以通过将睡眠持续时间减少到更小的秒数(我选择 5 秒)来快速观察到这一点。
在退出频道上发送,ch
, blocks
main
无法返回,因为用于传播退出请求的信号通道未缓冲并且没有相应的侦听器。通过改变线路:
ch := make(chan struct{})
to
ch := make(chan struct{}, 1)
通道上的发送main
将继续(到通道的缓冲区)并且main
将退出——与多个 goroutine 示例的行为相同。然而,这个实现仍然有问题:在实际开始停止子进程之前,不会从通道的缓冲区读取该值main
返回,因此子进程仍将是孤立进程。
固定版本
我已经为您制作了一个固定版本,代码如下。还有一些风格上的改进可以将您的示例转换为更惯用的 go:
package main
import (
"context"
"log"
"os/exec"
"sync"
"time"
)
func Run(ctx context.Context) {
cmd := exec.CommandContext(ctx, "sleep", "300")
err := cmd.Start()
if err != nil {
// Run could also return this error and push the program
// termination decision to the `main` method.
log.Fatal(err)
}
err = cmd.Wait()
if err != nil {
log.Println("waiting on cmd:", err)
}
}
func main() {
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())
// Increment the WaitGroup synchronously in the main method, to avoid
// racing with the goroutine starting.
wg.Add(1)
go func() {
Run(ctx)
// Signal the goroutine has completed
wg.Done()
}()
<-time.After(3 * time.Second)
log.Println("closing via ctx")
cancel()
// Wait for the child goroutine to finish, which will only occur when
// the child process has stopped and the call to cmd.Wait has returned.
// This prevents main() exiting prematurely.
wg.Wait()
}
(游乐场链接 https://play.golang.org/p/_i313PNpmKJ)