英文:
go routines and a channel to receive error or success
问题
我有一个函数,我想定义最大的goroutine数量。我有一个列表,我遍历这个列表,并通过通道向goroutine发送消息。在这个goroutine中,我将调用一个函数,该函数将返回一个答案或一个错误。当不是错误时,我想将返回值保存在一个切片中,当出现错误时,我想停止goroutine并进行调用。
但是我无法做到当出现错误时,所有的goroutine都结束,并且我需要错误的值。
type response struct {
	value string
}
func Testing() []response {
	fakeValues := getFakeValues()
	maxParallel := 25
	wg := &sync.WaitGroup{}
	wg.Add(maxParallel)
	if len(fakeValues) < maxParallel {
		maxParallel = len(fakeValues)
	}
	errReceive := make(chan error, 1)
	defer close(errReceive)
	response := make([]response, 0)
	valuesChan := make(chan string, 1)
	for i := 0; i < maxParallel; i++ {
		go func(valuesChan <-chan string, errReceive chan error) {
			for value := range valuesChan {
				resp, err := getFakeResult(value)
				if err != nil {
					errReceive <- err
				}
				response = append(response, resp)
			}
			wg.Done()
		}(valuesChan, errReceive)
	}
	for _, val := range fakeValues {
		valuesChan <- val
	}
	close(valuesChan)
	wg.Wait()
	err := <-errReceive
	if err != nil {
		// make any thing
	}
	return response
}
func getFakeValues() []string {
	return []string{"a", "b"}
}
func getFakeResult(val string) (response, error) {
	if val == "a" {
		return response{}, fmt.Errorf("ooh noh:%s", val)
	}
	return response{
		value: val,
	}, nil
}
以上是您提供的代码的翻译结果。
英文:
I have a function that I want to define a maximum number of go routines, I have a list and I go through this list and I send a message to the go routines through the channel, and in this go routine I will call a function that will either get an answer or an err, when it's not an err I want to save the return in a slice, and when it's an err I want to stop the go routines and make a call.
but I'm not able to make it so that when I have an error, all the go routines end, and I need the value of err
type response struct {
value string
}
func Testing() []response {
fakeValues := getFakeValues()
maxParallel := 25
wg := &sync.WaitGroup{}
wg.Add(maxParallel)
if len(fakeValues) < maxParallel {
maxParallel = len(fakeValues)
}
errReceive := make(chan error, 1)
defer close(errReceive)
response := make([]response, 0)
valuesChan := make(chan string, 1)
for i := 0; i < maxParallel; i++ {
go func(valuesChan <-chan string, errReceive chan error) {
for value := range valuesChan {
resp, err := getFakeResult(value)
if err != nil {
errReceive <- err
}
response = append(response, resp)
}
wg.Done()
}(valuesChan, errReceive)
}
for _, val := range fakeValues {
valuesChan <- val
}
close(valuesChan)
wg.Wait()
err := <-errReceive
if err != nil {
// make any thing
}
return response
}
func getFakeValues() []string {
return []string{"a", "b"}
}
func getFakeResult(val string) (response, error) {
if val == "a" {
return response{}, fmt.Errorf("ooh noh:%s", val)
}
return response{
value: val,
}, nil
}
答案1
得分: 1
你可以使用带有cancel的上下文(context)来通知goroutine停止执行。
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
wg := &sync.WaitGroup{}
wg.Add(1)
go func(ctx context.Context) {
	defer wg.Done()
	for {
		select {
		case <-ctx.Done():
			fmt.Println("context is done")
			return
		case <-time.After(time.Second):
			fmt.Println("work")
		}
	}
}(ctx)
time.Sleep(time.Second * 5)
cancel()
wg.Wait()
这是一个更好地展示了上下文在你的用例中的示例。
type result struct {
	err error
	val int
}
rand.Seed(time.Now().UnixNano())
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
rchan := make(chan result, 5)
wg := &sync.WaitGroup{}
for i := 0; i < 5; i++ {
	wg.Add(1)
	go func(ctx context.Context) {
		defer wg.Done()
		for {
			select {
			case <-ctx.Done():
				fmt.Println("context is done")
				return
			case <-time.After(time.Second):
				n := rand.Intn(100)
				if n > 90 {
					rchan <- result{err: fmt.Errorf("error %d", n)}
				} else {
					rchan <- result{val: n}
				}
			}
		}
	}(ctx)
}
go func() {
	wg.Wait()
	close(rchan)
}()
for res := range rchan {
	if res.err != nil {
		fmt.Println(res.err)
		cancel()
		break
	} else {
		fmt.Println(res.val)
	}
}
你可以在这里查看代码:https://go.dev/play/p/Z63n1h2A81o
英文:
You can use a context with cancel and use it to let the go routines know they should stop.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
wg := &sync.WaitGroup{}
wg.Add(1)
go func(ctx context.Context) {
defer wg.Done()
for {
select {
case <-ctx.Done():
fmt.Println("context is done")
return
case <-time.After(time.Second):
fmt.Println("work")
}
}
}(ctx)
time.Sleep(time.Second * 5)
cancel()
wg.Wait()
https://go.dev/play/p/qe2oDppSnaF
Here is an example that showcases it better in the context of your use case.
type result struct {
err error
val int
}
rand.Seed(time.Now().UnixNano())
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
rchan := make(chan result, 5)
wg := &sync.WaitGroup{}
for i := 0; i < 5; i++ {
wg.Add(1)
go func(ctx context.Context) {
defer wg.Done()
for {
select {
case <-ctx.Done():
fmt.Println("context is done")
return
case <-time.After(time.Second):
n := rand.Intn(100)
if n > 90 {
rchan <- result{err: fmt.Errorf("error %d", n)}
} else {
rchan <- result{val: n}
}
}
}
}(ctx)
}
go func() {
wg.Wait()
close(rchan)
}()
for res := range rchan {
if res.err != nil {
fmt.Println(res.err)
cancel()
break
} else {
fmt.Println(res.val)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论