如何在 select 语句的父作用域中调用一个在 select case 中运行的 goroutine?

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

How to call a goroutine inside of a select case that runs in the scope of the select's parent

问题

我正在构建一个数据工具,它可以在流中收集数据并对其进行操作。我有一个主例程,一个“处理管理器”,负责创建累积函数的新例程。管理器根据一个通道接收选择语句来创建例程,该选择语句在一个无限的for循环中运行(我已经为它自身和它创建的所有例程编写了取消例程)。问题是,管理器需要能够在其主范围内运行goroutine累加器,以便它们可以在选择和for循环的范围之外运行(我希望它们在管理器接受新案例时继续运行)。

cancel := make(chan struct{})
chanchannel := make(chan chan datatype)

func operationManager(chanchannel chan chan datatype, cancel chan struct{}) {
    for {
        select {
        case newchan := <-chanchannel:
            go runAccum(newchan, cancel)
        case <-cancel:
            return
        }
    }
}

func runAccum(inchan chan datatype, cancel chan struct{}) {
    for {
        select {
        case data := <-inchan:
            //do something
        case <-cancel:
            return
        }
    }
}

这只是我使用情况的一个非常简化的示例,但我希望它能说明我的问题的组成部分。如果这是可能的、可行的、合理的、不可取的,请告诉我;不,这不是我实现拆除的方式哈哈。

英文:

I am building a data tool that collects data in a stream and operates on it. I have a main routine, a "process manager", which is responsible for creating new routines of an accumulation function. The manager is informed to create the routines based on a channel receive select case which it is running in an infinite for loop (I already have my cancel routine for itself and all of the routines it creates). The problem is that the manager needs to be able to run the goroutine accumulators in its main scope so that they can operate outside of the select and for loop's scope (I want them to keep running while the manager accepts new cases).

cancel := make(chan struct{})
chanchannel := make(chan chan datatype)

func operationManager (chanchannel chan chan datatype, cancel chan struct{}) {
for {
 select {
  case newchan := &lt;- chanchannel:
    go runAccum(newchan, cancel)
  case &lt;- cancel:
    return
 }
}
}

func runAccum(inchan chan datatype, cancel chan struct{}) {
for {
 select {
  case data := &lt;- inchan;
    //do something
  case &lt;- cancel:
    return
 }
}
}

This is a very, very dumbed-down example of my use-case, but I hope it illustrates my problem's component pieces. Let me know if this is possible, feasible, reasonable, inadvisable; And no, this is not how I implemented my teardown haha

答案1

得分: 1

协程(goroutine)没有“作用域”的概念。所有的协程都是平等的。
闭包有“作用域”的概念,但是你的协程并不跨越闭包。
所以,通过go runAccum(newchan, cancel)启动的所有协程,与你从任何地方启动的其他协程一样。

我猜你没有测试你的解决方案?

英文:

There is no "scope" to goroutines. All goroutines are equal.
There is "scope" for closures but your goroutines do not span closures.
So all your goroutines spanned by go runAccum(newchan, cancel) will be like any other goroutine you span, no matter from where.

I assume you did not test your solution?

huangapple
  • 本文由 发表于 2014年1月15日 16:04:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/21132051.html
匿名

发表评论

匿名网友

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

确定