我目前有這樣的東西
type Foo struct {
rpChan chan<- *Data
}
func (s *Foo) Work() {
......
for event := range watch.ResultChan() {
s.rpChan <- &sr
...........
...........
}
}
然后在其他地方我像這樣從該通道(rpChan)中提取資料
func (r *Bar) process() {
for t := range r.reqChan {
//How do I Pause writing more stuff to this channel ? It has just been unblocked
r.processEvent(t)
//Un-Pause writing more stuff to this channel - Now send me the next thing.
}
}
我的問題是告訴頻道在完成之前停止向其寫入更多內容的最佳方法是什么ProcessEvent?由于該process方法只是從中提取了一些東西r.reqChan,因此我不希望 Foo Work() 在完成之前將更多資料寫入rpChan通道processEvent。我唯一的想法是引入另一個通道,該通道在 r.processEvent(t)
完成時設定,然后process從該通道讀取以繼續。有更好的方法嗎?也許是 IPC 佇列?
uj5u.com熱心網友回復:
規范說 如果容量為零或不存在,則通道是無緩沖的,并且只有在發送方和接收方都準備好時通信才會成功。
創建一個無緩沖通道以確保在接收 goroutine 執行時r.reqChan發送到r.reqChan不完成r.processEvent(t)。
goroutine 一次只能做一件事。如果接收 goroutine 正在執行r.processEvent(t),那么接收 goroutine 沒有執行隱含的接收操作range。因此,在接收 goroutine 執行時,發送沒有完成r.processEvent(t)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/526626.html
標籤:去
上一篇:(discord.jsv14)MongoDB沒有正確保存用戶不和諧ID
下一篇:Golang構建多平臺問題
