有些东西没有被冲洗.发生了什么的简化示例:
def testDemo() {
def person = new Person(...)
person.save(flush: true)
println "Number of people after save: " + Person.all.size()
def dummyList = [1, 2, 3, 4, 5]
GParsPool.withPool { num ->
println "Number of people after withPool: " + Person.all.size()
dummyList.eachParallel {
println "Number of people after eachParallel " + Person.all.size()
Person.withTransaction {
...
Run Code Online (Sandbox Code Playgroud)
这输出:
Number of people after save: 1
Number of people after withPool: 1
Number of people after eachParallel: 0
Run Code Online (Sandbox Code Playgroud)
我不明白我是否必须使用Session和Transaction来使数据保持不变或者这是GPars中的错误.底层的hibernate级别发生了什么?
我希望最近创建的Person在并行闭包中可见.
我认为这很容易找到,但我失败了.
如果我在我的Groovy应用程序中使用GPars并且我没有指定池大小将创建多少个线程?是否有默认池大小而没有设置一个?
// How many threads will be created? What is the default pool size?
GParsExecutorsPool.withPool {
// do stuff...
}
Run Code Online (Sandbox Code Playgroud) 我是GPARS库的新手,目前正在我们的软件中实现它.
对我来说,使用它而不是像普通的groovy方法一样没问题
[..].each{..}
->
[..].eachParallel{..}
Run Code Online (Sandbox Code Playgroud)
但我想知道如何并行化2个返回值的任务.
没有GPARS,我会这样做:
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
Run Code Online (Sandbox Code Playgroud)
但是(如何)可以用GparsPool完成?
当我在控制台中运行以下代码时(groovy 2.1.3):
strings = [ "butter", "bread", "dragon", "table" ]
strings.eachParallel{println "$it0"}
Run Code Online (Sandbox Code Playgroud)
我明白了:
groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.eachParallel() is applicable for argument types: (ConsoleScript40$_run_closure1) values: [ConsoleScript40$_run_closure1@a826f5]
Run Code Online (Sandbox Code Playgroud)
谁能告诉我我做错了什么?
我想在多个线程中运行相同的Cucumber测试.更具体地说,我有一组功能,在一个线程中运行这些功能可以正常工作.我使用JSON格式化程序来记录每个步骤的运行时间.现在我想做负载测试.我更关心多线程环境中每个功能/步骤的运行时间.所以我创建了多个线程,每个线程都运行在同一个功能集上.每个线程都有自己的JSON报告.理论上这可能吗?
对于某些项目设置原因,我无法使用JUnit运行程序.所以我不得不采用CLI方式:
long threadId = Thread.currentThread().getId();
String jsonFilename = String.format("json:run/cucumber%d.json", threadId);
String argv[] = new String[]{
"--glue",
"com.some.package",
"--format",
jsonFilename,
"d:\\features"};
// Do not call Main.run() directly. It has a System.exit() call at the end.
// Main.run(argv, Thread.currentThread().getContextClassLoader());
// Copied the same code from Main.run().
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
RuntimeOptions runtimeOptions = new RuntimeOptions(new Env("cucumber-jvm"), argv);
ResourceLoader resourceLoader = new MultiLoader(classLoader);
ClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, classLoader);
Runtime runtime = new Runtime(resourceLoader, classFinder, classLoader, runtimeOptions);
runtime.writeStepdefsJson();
runtime.run();
Run Code Online (Sandbox Code Playgroud)
我试图为每个Cucumber运行创建一个单独的线程.问题是,只有一个线程有一个有效的JSON报告.所有其他线程只创建空JSON文件.这是黄瓜的设计还是我错过了什么?
我有一个大型的处理任务,我相信它已经成熟,可以提高并发性和并行性.
我看了一下GPars文档,发现它们很混乱,所以我希望这里的人能提供帮助.
我想要并行执行的第一项任务目前看起来像这样:
def providerOneProgrammes = providerOneProgrammeService.getProgrammes(timeWindow)
def providerTwoProgrammes = providerTwoProgrammeService.getProgrammes(timeWindow)
Run Code Online (Sandbox Code Playgroud)
两者都返回一个对象列表,两者都可以并行运行.
我想一起执行它们然后在处理返回列表之前等待它们完成(然后我会查找列表之间的匹配但我稍后会讨论).
谢谢
拉克什
我想用这些示例字符串中的每一个做很多东西,并在这里返回一些其他类型的Object Integers,稍后是一些更大的类对象.
在这个例子中,我正在尝试一些简单的事情,但是我怎么会得到一个完全错误的结果.至少对于我希望得到的东西.的xD
我希望得到:[6, 5, 6, 5]
但我得到:[butter, bread, dragon, table]
package test
@Grab(group='org.codehaus.gpars', module='gpars', version='1.0.0')
import static groovyx.gpars.GParsPool.withPool
class Test {
List<String> strings = new ArrayList<String>([
"butter",
"bread",
"dragon",
"table"
])
def closure = { it.length() }
def doStuff() {
def results = withPool( 4 ) {
strings.eachParallel{ it.length()}
}
println results
}
static main(args) {
def test = new Test()
test.doStuff()
}
}
Run Code Online (Sandbox Code Playgroud)
如果答案可以有一个简短的解释,那将是很好的.非常感谢!
我找到了一个如何使用的例子withPool
.它说我只需要将这个词添加Parallel
到Groovy的方法中,比如说collect, find, each
,将它放入withPool
并执行代码并行.
import static groovyx.gpars.GParsPool.withPool
list = 1..1000000
withPool{
squares = list.collectParallel { it * it}
}
println squares
Run Code Online (Sandbox Code Playgroud)
有没有机会检查它是否真的平行?
我尝试使用相同的代码进行基准测试,但顺序方式
和并行方式要慢得多.
我正在使用Grails 1.4.0.M1,我正在尝试升级gpars的版本以与我的应用程序一起使用.
所以我在BuildConfig.groovy中添加了:
dependencies{
build 'org.codehaus.gpars:gpars:0.11'
}
Run Code Online (Sandbox Code Playgroud)
但是当我使用run-app时,这会给我带来以下异常:
Exception in thread "Thread-4" java.lang.ClassCastException: groovyx.gpars.ThreadLocalPools cannot be cast to groovy.lang.GroovyObject
at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:231)
at groovyx.gpars.Parallelizer.retrieveCurrentPool(Parallelizer.groovy:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Run Code Online (Sandbox Code Playgroud)
我怀疑它可以链接到grails框架,在旧版本中使用此lib并且版本不兼容.
知道如何克服这个问题吗?
谢谢.