在循环内触发一个消费通道。

huangapple go评论88阅读模式
英文:

Trigger a channel inside the loop where the the channel is consumed

问题

如何在循环内部触发一个消费相同通道的通道。下面是一个不起作用的示例代码。如何实现这个目标?

package main

import (
	"context"
	"fmt"
	"time"
)

func main() {
	ch1 := make(chan struct{})
	ch2 := make(chan struct{})
	defer close(ch1)
	defer close(ch2)

	ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
	defer cancel()

	go func() {
		time.Sleep(time.Second * 1)
		ch1 <- struct{}{}
	}()

loop:
	for {
		select {
		case <-ctx.Done():
			fmt.Println("timeout")
			break loop
		case <-ch1:
			fmt.Println("ch1")
			ch2 <- struct{}{} // 这里不起作用!
		case <-ch2:
			fmt.Println("ch2")
		}
	}

}
英文:

How to rigger a channel inside the loop where the same channel is consumed. Below is a sample code that does not work. How is this achievable?

https://go.dev/play/p/o5ZhNfw4IFu

package main

import (
	&quot;context&quot;
	&quot;fmt&quot;
	&quot;time&quot;
)

func main() {
	ch1 := make(chan struct{})
	ch2 := make(chan struct{})
	defer close(ch1)
	defer close(ch2)

	ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
	defer cancel()

	go func() {
		time.Sleep(time.Second * 1)
		ch1 &lt;- struct{}{}
	}()

loop:
	for {
		select {
		case &lt;-ctx.Done():
			fmt.Println(&quot;timeout&quot;)
			break loop
		case &lt;-ch1:
			fmt.Println(&quot;ch1&quot;)
			ch2 &lt;- struct{}{} // This here does not work!
		case &lt;-ch2:
			fmt.Println(&quot;ch2&quot;)
		}
	}

}

答案1

得分: 1

1. 在goroutine内部向ch2发送数据

package main

import (
	"context"
	"fmt"
	"time"
)

func main() {
	ch1 := make(chan struct{})
	ch2 := make(chan struct{})
	defer close(ch1)
	defer close(ch2)

	ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
	defer cancel()

	go func() {
		time.Sleep(time.Second * 1)
		ch1 <- struct{}{}
	}()

loop:
	for {
		select {
		case <-ctx.Done():
			fmt.Println("超时")
			break loop
		case <-ch1:
			fmt.Println("ch1")
			go func() {
				ch2 <- struct{}{}
			}()
		case <-ch2:
			fmt.Println("ch2")
		}
	}

}

或者

2. 将ch2设置为带缓冲的通道

package main

import (
	"context"
	"fmt"
	"time"
)

func main() {
	ch1 := make(chan struct{})
	ch2 := make(chan struct{}, 1)
	defer close(ch1)
	defer close(ch2)

	ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
	defer cancel()

	go func() {
		time.Sleep(time.Second * 1)
		ch1 <- struct{}{}
	}()

loop:
	for {
		select {
		case <-ctx.Done():
			fmt.Println("超时")
			break loop
		case <-ch1:
			fmt.Println("ch1")
			ch2 <- struct{}{}
		case <-ch2:
			fmt.Println("ch2")
		}
	}

}
英文:

1. send data to ch2 inside goroutine

package main

import (
	&quot;context&quot;
	&quot;fmt&quot;
	&quot;time&quot;
)

func main() {
	ch1 := make(chan struct{})
	ch2 := make(chan struct{})
	defer close(ch1)
	defer close(ch2)

	ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
	defer cancel()

	go func() {
		time.Sleep(time.Second * 1)
		ch1 &lt;- struct{}{}
	}()

loop:
	for {
		select {
		case &lt;-ctx.Done():
			fmt.Println(&quot;timeout&quot;)
			break loop
		case &lt;-ch1:
			fmt.Println(&quot;ch1&quot;)
			go func() {
				ch2 &lt;- struct{}{}
			}()
		case &lt;-ch2:
			fmt.Println(&quot;ch2&quot;)
		}
	}

}

or

2. make ch2 buffered

package main

import (
	&quot;context&quot;
	&quot;fmt&quot;
	&quot;time&quot;
)

func main() {
	ch1 := make(chan struct{})
	ch2 := make(chan struct{}, 1)
	defer close(ch1)
	defer close(ch2)

	ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
	defer cancel()

	go func() {
		time.Sleep(time.Second * 1)
		ch1 &lt;- struct{}{}
	}()

loop:
	for {
		select {
		case &lt;-ctx.Done():
			fmt.Println(&quot;timeout&quot;)
			break loop
		case &lt;-ch1:
			fmt.Println(&quot;ch1&quot;)
			ch2 &lt;- struct{}{}
		case &lt;-ch2:
			fmt.Println(&quot;ch2&quot;)
		}
	}

}

huangapple
  • 本文由 发表于 2022年3月19日 17:27:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/71536936.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定