2 回答

TA貢獻(xiàn)1826條經(jīng)驗(yàn) 獲得超6個(gè)贊
此代碼將永遠(yuǎn)不會(huì)退出,直到它進(jìn)行l(wèi)en(queues)多次處理。它不是并發(fā)代碼 - 全部在主體中 - 并且沒(méi)有信號(hào)告訴代碼停止。問(wèn)題在這里:
case "run":
// Installing the service
installed, err := service.Install()
logError(err, installed)
// Starting the service
started, err := service.Start()
logError(err, started)
if err == nil {
// Creating a goroutine and executing the queue's processes in parallel
for i := 0; i < len(queues); i++ {
go startProcessing(queues[i])
time.Sleep(time.Second) // Waiting for other functions to execute
}
select {} // To prevent the goroutine from exiting the main func
}
fmt.Println(started)
可以看出,這select{}條線將坐在那里并永遠(yuǎn)運(yùn)行!:) 最好將這個(gè) case 子句移到他們自己的 goroutine 中,并在那里有一個(gè)退出信號(hào),如下所示:
select {
case <-quit:
return
}
盡管這不是在 Go 應(yīng)用程序中處理啟動(dòng)/停止的最簡(jiǎn)潔方式;它只是顯示了問(wèn)題。
- 2 回答
- 0 關(guān)注
- 351 瀏覽
添加回答
舉報(bào)