我有一個 goroutine 用于長期運行的工作。工作完成后,它將結果推送到通道。與此同時,當作業正在運行時,我想繼續更新狀態為 RUNNING 的 API。到目前為止,我有以下代碼:func getProgressTimeout() <-chan time.Time { return time.After(5 * time.Minute)}func runCommand(arg *Request) { chanResult := make(chan Results) go func(args *Request, c chan Results) { resp, err := execCommand(args) c <- Results{ resp: resp, err: err, } }(arg, chanResult) var err errorprogressLoop: for { select { case <-getProgressTimeout(): updateProgress() // this method will send status= RUNNING to a REST API case out := <-chanResult: err = jobCompleted(request, out) break progressLoop } } return err}我是戈朗的新手。經過大量的反復試驗和谷歌搜索,我已經達到了上面的代碼。它現在正在工作。當我看到它時,我仍然覺得它不直觀(這很可能是因為,我仍在努力學習 Go 的做事方式)。所以我的問題是,我可以將其重構為更好的形狀嗎?是否有一些適用于這種情況的現有模式?或者是否有一些完全不同的方法可以在作業運行時持續發送定期更新?此外,也感謝任何改進我的 golang 并發性的建議。:)
1 回答

海綿寶寶撒
TA貢獻1809條經驗 獲得超8個贊
考慮使用time.NewTicker,它向通道發送周期值。這是文檔中的示例:
package main
import (
? ? "fmt"
? ? "time"
)
func main() {
? ? ticker := time.NewTicker(time.Second)
? ? defer ticker.Stop()
? ? done := make(chan bool)
? ? go func() {
? ? ? ? time.Sleep(10 * time.Second)
? ? ? ? done <- true
? ? }()
? ? for {
? ? ? ? select {
? ? ? ? case <-done:
? ? ? ? ? ? fmt.Println("Done!")
? ? ? ? ? ? return
? ? ? ? case t := <-ticker.C:
? ? ? ? ? ? fmt.Println("Current time: ", t)
? ? ? ? }
? ? }
}
請注意,嵌入式 goroutine 調用func通過休眠 10 秒來模擬一個長任務,而調用者用來select等待結果,同時還從自動收報機接收周期性事件——這是您可以進行 API 進度更新的地方。
- 1 回答
- 0 關注
- 116 瀏覽
添加回答
舉報
0/150
提交
取消