Groovy GParsExecutorsPool.withPool未进行异步连接
我正在使用groovy和GPAR进行异步连接。我得到一个巨大的JSON作为对API的请求,我使用JSON路径拆分JSON。 $.${jsonobjstpath}..[i][j],其中i和j是0:20范围内的值,并在其上循环。我能够得到正确的拆分json。 我使用GParsExecutorsPool.withPool将这些JSON批发送到我的API。但gpar正在等待响应。 比方说,若处理一个请求的API需要10秒,gpar将等待10秒将控制器发送到循环。我的代码如下Groovy GParsExecutorsPool.withPool未进行异步连接,groovy,gpars,Groovy,Gpars,我正在使用groovy和GPAR进行异步连接。我得到一个巨大的JSON作为对API的请求,我使用JSON路径拆分JSON。 $.${jsonobjstpath}..[i][j],其中i和j是0:20范围内的值,并在其上循环。我能够得到正确的拆分json。 我使用GParsExecutorsPool.withPool将这些JSON批发送到我的API。但gpar正在等待响应。 比方说,若处理一个请求的API需要10秒,gpar将等待10秒将控制器发送到循环。我的代码如下 import gro
import groovyx.gpars.GParsExecutorsPool;
import groovyx.gpars.GParsPool;
import jsr166y.ForkJoinPool;
import jsr166y.RecursiveTask;
def invoke(mega) {
def size=mega.get("size"); //get size of actual JSON objects
def body=mega.get("content.body"); // Load JSON body
int counter=Math.ceil(size/20); //Get Number of loops to run
def Path="/AsyncInmp"; // Path to call function
def Name="SplitJsonObject"; //Name of Function
int i=0;
int j=19;
while(j<=size) {
msg.put("i",i); //Send i value to function
msg.put("j",j); // Send j value to function
callPolicy(Path,Name,body); //Call function json path to get split json, receiving JSON with i and j values
def split_body=resp.body;//response from split json
def Path2="/AsyncInmp"; //path to connection function
def Name2="connect"; //name of connection function
GParsExecutorsPool.withPool {
(0..<1).eachParallel { k ->
callPolicy(Path2, Name2,split_body) //Call function to connect using gpars, This is not working
}
}
j=j+20;
i=i+20;
}
return true;
}
导入groovyx.gpars.GParsExecutorsPool;
导入groovyx.gpars.GParsPool;
导入jsr166y.ForkJoinPool;
导入jsr166y.RecursiveTask;
def invoke(mega){
def size=mega.get(“size”);//获取实际JSON对象的大小
def body=mega.get(“content.body”);//加载JSON body
int counter=Math.ceil(size/20);//获取要运行的循环数
def Path=“/AsyncInmp”;//调用函数的路径
def Name=“SplitJsonObject”;//函数名
int i=0;
int j=19;
当(j您在while
循环中调用with pool
,并且在每个并行中使用大小为1的范围时,我猜这些因素的结合基本上使您的代码以单线程方式运行
将其更改为以下内容:
import java.util.concurrent.CopyOnWriteArrayList
def futures = [] as CopyOnWriteArrayList
GParsExecutorsPool.withPool {
while(...) {
...
futures << {
callPolicy(Path2, Name2,split_body)
}.async().call()
}
}
// wait for all requests to complete
def results = futures*.get() // or futures.collect { it.get() } if this breaks
// results is now a list of return values from callPolicy
我们可以看到结果被返回,调用也是以多线程的方式进行的,因为序列
和索引
值不是按顺序进行的。您在循环中调用池
,同时在每个并行中使用大小为1的范围ode>,我猜这些东西的结合本质上使您的代码以单线程的方式运行
将其更改为以下内容:
import java.util.concurrent.CopyOnWriteArrayList
def futures = [] as CopyOnWriteArrayList
GParsExecutorsPool.withPool {
while(...) {
...
futures << {
callPolicy(Path2, Name2,split_body)
}.async().call()
}
}
// wait for all requests to complete
def results = futures*.get() // or futures.collect { it.get() } if this breaks
// results is now a list of return values from callPolicy
在这里,我们可以看到返回的结果以及调用是以多线程方式进行的,因为序列
和索引
值不是都按顺序进行的。谢谢Matias,我能够根据您提供的信息调整我的代码,可以将批发送到后端系统并获得响应。我面临回答中的一个问题是,在未来,我将作为响应而不是JSON响应变为真,这是我的响应[真的,真的,真的,真的,真的,真的,真的,真的,真的,真的,真的,真的]你试过打印callPolicy
的返回值以确保它不返回布尔值吗?是的,试过了,它给出了正确的响应,意味着json正文,但futures只存储trueMatias,我可以根据你提供的信息调整我的代码,可以将批发送到后端系统并获得响应。我面临回答中的一个问题是,在未来,我将作为响应而不是JSON响应变为真,这是我的响应[真的,真的,真的,真的,真的,真的,真的,真的,真的,真的,真的,真的]您是否尝试过打印callPolicy
的返回值以确保它没有返回布尔值?是的,尝试过了,它给出了正确的响应,意味着json正文,但futures只存储true
~> groovy solution.groovy
[index:0, sequence:9, time:1558893973348]
[index:1, sequence:1, time:1558893973305]
[index:2, sequence:8, time:1558893973337]
[index:3, sequence:5, time:1558893973322]
[index:4, sequence:7, time:1558893973337]
[index:5, sequence:4, time:1558893973320]
[index:6, sequence:3, time:1558893973308]
[index:7, sequence:6, time:1558893973332]
[index:8, sequence:0, time:1558893973282]
[index:9, sequence:2, time:1558893973308]
~>