Groovy/Grails GPAR:如何并行执行2个计算?

Groovy/Grails GPAR:如何并行执行2个计算?,groovy,parallel-processing,gpars,Groovy,Parallel Processing,Gpars,我是GPARS库的新手,目前正在我们的软件中实现它 对我来说,使用它代替常规的groovy方法(如 [..].each{..} -> [..].eachParallel{..} 但我想知道如何并行化两个返回值的任务 如果没有GPAR,我会这样做: List<Thread> threads = [] def forecastData def actualData threads.add(Thread.start { forecastData = cosmoSeg

我是GPARS库的新手,目前正在我们的软件中实现它

对我来说,使用它代替常规的groovy方法(如

[..].each{..} 
-> 
[..].eachParallel{..}
但我想知道如何并行化两个返回值的任务

如果没有GPAR,我会这样做:

List<Thread> threads = []
def forecastData
def actualData  
threads.add(Thread.start {
    forecastData = cosmoSegmentationService.getForecastSegmentCharacteristics(dataset, planPeriod, thruPeriod)
})

threads.add(Thread.start {
    actualData = cosmoSegmentationService.getMeasuredSegmentCharacteristics(dataset, fromPeriod, thruPeriodActual)
})

threads*.join()

// merge both datasets
def data = actualData + forecastData
列出线程=[]
def预报数据
def实际数据
threads.add(Thread.start{
forecastData=cosmoSegmentationService.getForecastSegmentCharacteristics(数据集、planPeriod、thruPeriod)
})
threads.add(Thread.start{
actualData=cosmoSegmentationService.getMeasuredSegmentCharacteristics(数据集、fromPeriod、ThroughPeriodActual)
})
线程*.join()
//合并两个数据集
def数据=实际数据+预测数据

但是(如何)使用GparsPool可以做到这一点呢?

您可以使用数据流:

import groovyx.gpars.dataflow.*
import static groovyx.gpars.dataflow.Dataflow.task

def forecastData = new DataflowVariable()
def actualData = new DataflowVariable()
def result = new DataflowVariable()

task {
  forecastData << cosmoSegmentationService.getForecastSegmentCharacteristics( dataset, planPeriod, thruPeriod )
}

task {
  actualData << cosmoSegmentationService.getMeasuredSegmentCharacteristics( dataset, fromPeriod, thruPeriodActual )
}

task {
  result << forecastData.val + actualData.val
}

println result.val

非常感谢你。不幸的是,我现在有一个问题,我们的Grails1.3.7与GPars0.9捆绑在一起,这导致了数据流方法的问题。但是你的回答是对的,谢谢你。@Martin使用
withPool
添加了一个替代选项,它似乎适用于GPars 0.9:-)难以置信:)非常感谢!顺便说一句:在Grails1.3.7中包含了GPars0.9,但作为一个剥离的jar。要使用GParsPool,我包括gpars0.12。因为Grails 1.3.7的剥离jar版本中没有GParsPool,这是可行的brrrr数据流方法在剥离jar中,但是是的,剥离且不可用。@tim_yates非常感谢!带有
GParsPool
的seconds示例非常有效。我直到现在才看到数据流。非常感谢分享!
import static groovyx.gpars.GParsPool.withPool

def getForecast = {
  cosmoSegmentationService.getForecastSegmentCharacteristics( dataset, planPeriod, }

def getActual = {
  cosmoSegmentationService.getMeasuredSegmentCharacteristics( dataset, fromPeriod, thruPeriodActual )
}

def results = withPool {
  [ getForecast.callAsync(), getActual.callAsync() ]
}

println results*.get().sum()
import groovyx.gpars.GParsPool    

List todoList =[]

todoList.add {
    for(int i1: 1..100){
        println "task 1:" +i1
        sleep(300)
    }
}
todoList.add {
    for(int i2: 101..200){
        println "task 2:" +i2
        sleep(300)
    }
}

GParsPool.withPool(2) {
    todoList.collectParallel { closure->closure() }
}