生产者速度慢,消费者速度快的情况下如何处理通道关闭同步?
How to handle channel closing synchronisation in case of slow producer, fast consumer in go?
我是新手,找不到这个问题的答案。我正在做的是在生产者中读取 CSV 文件,做一些可能需要时间的事情,然后通过通道将输出发送给消费者。有一条 生产者-消费者 链,任何生产者最终都可能比它的消费者慢。
producer(1 goroutine) -> chan0 -> consumer-producer-1(>1 goroutines) -> chan1 ->
consumer-producer-2(>1 goroutines) -> chan2 -> consumer(>1 goroutines)
此处最多可容纳 15 个消费者。
现在我面临的问题是,如果生产者完成了,消费者端如何决定,我们可以停止处理。
我需要实现的是:
- 一旦生产者完成,所有消费者最终应该做一些清理并在完成剩余的操作后退出
- 如果消费者在特定的超时期限内没有获得任何数据,它可以退出(最好有信号)而不会进一步阻塞。
- 序列中的所有生产者-消费者对都会发生这种情况。
我使用了以下方法。
- 为每个数据通道保留一个信号通道,并为其下一个消费者的每个 goroutine 发布一个 "done"。
- 读取后,每个消费者应该只读取通道中剩余的缓冲数据,然后将 5 "done" 放入下一个信号通道。确保每个协程只有 5 个,而不是 5 个(使用 https://golang.org/pkg/sync/#Once.Do)。
以下是我到这里为止能想到的。
processRemaining = false
for processRemaining == false{
select {
case stuff, ok := <-input_messages:
do_stuff(stuff)
if ok == false { // if channel has been closed
processRemaining = true
}
if result != nil {
//send to channel output_messages
}
case sig := <-input_signals: // if signaled to stopped.
fmt.Println("received signal", sig)
processRemaining = true
default:
fmt.Println("no activity")
}
}
if processRemaining {
for stuff := range input_messages {
do_stuff(stuff)
if result != nil {
//send to channel output_messages
}
}
// send "output_routine" number of "done" to a channel "output_signals".
}
但即使在这种方法中,我也无法想出任何方式来表现与关闭 "input_messages" 频道相同的方式,如果没有可用的时间,比如 10 秒。
这种方法有没有我忽略的问题。解决这个问题的可能方法(或并发模式)是什么?确保:
- 所有后续通道都关闭,一旦第一个"chan0"关闭。
- 所有生产者在关闭他们的输出通道之前都会更新,并且只有在他们都完成写入后才会关闭通道。
- 如果消费者在指定的超时时间内没有从通道获取数据,它应该将其视为已关闭,并自行解除阻塞。
使用 sync.WaitGroup
来跟踪 运行 goroutines 的数量。每个 goroutine 在不再从通道获取数据后退出。 WaitGroup
完成后,就可以进行清理了。
像这样:
import (
"sync"
"time"
)
type Data interface{} // just an example
type Consumer interface {
Consume(Data) Data
CleanUp()
Count() int
Timeout() time.Duration
}
func StartConsumers(consumer Consumer, inCh <-chan Data, outCh chan<- Data) {
wg := sync.WaitGroup{}
for i := 0; i < consumer.Count(); i++ {
wg.Add(1)
go func() {
consumeLoop:
for {
select {
case v, ok := <-inCh: // 'ok' says if the channel is still open
if !ok {
break consumeLoop
}
outCh <- consumer.Consume(v)
case <-time.After(consumer.Timeout()):
break consumeLoop
}
}
wg.Done()
}()
}
wg.Wait()
consumer.CleanUp()
close(outCh)
}
在管道的每个阶段,您都可以使用与上述类似的过程来启动消费者。
我是新手,找不到这个问题的答案。我正在做的是在生产者中读取 CSV 文件,做一些可能需要时间的事情,然后通过通道将输出发送给消费者。有一条 生产者-消费者 链,任何生产者最终都可能比它的消费者慢。
producer(1 goroutine) -> chan0 -> consumer-producer-1(>1 goroutines) -> chan1 -> consumer-producer-2(>1 goroutines) -> chan2 -> consumer(>1 goroutines)
此处最多可容纳 15 个消费者。
现在我面临的问题是,如果生产者完成了,消费者端如何决定,我们可以停止处理。
我需要实现的是:
- 一旦生产者完成,所有消费者最终应该做一些清理并在完成剩余的操作后退出
- 如果消费者在特定的超时期限内没有获得任何数据,它可以退出(最好有信号)而不会进一步阻塞。
- 序列中的所有生产者-消费者对都会发生这种情况。
我使用了以下方法。
- 为每个数据通道保留一个信号通道,并为其下一个消费者的每个 goroutine 发布一个 "done"。
- 读取后,每个消费者应该只读取通道中剩余的缓冲数据,然后将 5 "done" 放入下一个信号通道。确保每个协程只有 5 个,而不是 5 个(使用 https://golang.org/pkg/sync/#Once.Do)。
以下是我到这里为止能想到的。
processRemaining = false for processRemaining == false{ select { case stuff, ok := <-input_messages: do_stuff(stuff) if ok == false { // if channel has been closed processRemaining = true } if result != nil { //send to channel output_messages } case sig := <-input_signals: // if signaled to stopped. fmt.Println("received signal", sig) processRemaining = true default: fmt.Println("no activity") } } if processRemaining { for stuff := range input_messages { do_stuff(stuff) if result != nil { //send to channel output_messages } } // send "output_routine" number of "done" to a channel "output_signals". }
但即使在这种方法中,我也无法想出任何方式来表现与关闭 "input_messages" 频道相同的方式,如果没有可用的时间,比如 10 秒。
这种方法有没有我忽略的问题。解决这个问题的可能方法(或并发模式)是什么?确保:
- 所有后续通道都关闭,一旦第一个"chan0"关闭。
- 所有生产者在关闭他们的输出通道之前都会更新,并且只有在他们都完成写入后才会关闭通道。
- 如果消费者在指定的超时时间内没有从通道获取数据,它应该将其视为已关闭,并自行解除阻塞。
使用 sync.WaitGroup
来跟踪 运行 goroutines 的数量。每个 goroutine 在不再从通道获取数据后退出。 WaitGroup
完成后,就可以进行清理了。
像这样:
import (
"sync"
"time"
)
type Data interface{} // just an example
type Consumer interface {
Consume(Data) Data
CleanUp()
Count() int
Timeout() time.Duration
}
func StartConsumers(consumer Consumer, inCh <-chan Data, outCh chan<- Data) {
wg := sync.WaitGroup{}
for i := 0; i < consumer.Count(); i++ {
wg.Add(1)
go func() {
consumeLoop:
for {
select {
case v, ok := <-inCh: // 'ok' says if the channel is still open
if !ok {
break consumeLoop
}
outCh <- consumer.Consume(v)
case <-time.After(consumer.Timeout()):
break consumeLoop
}
}
wg.Done()
}()
}
wg.Wait()
consumer.CleanUp()
close(outCh)
}
在管道的每个阶段,您都可以使用与上述类似的过程来启动消费者。